Using AngularJS to interact with resources by passing in the scope parameter

My issue is as follows: I am attempting to call a resource with a specific parameter, but I keep encountering the following error:

[$resource:badcfg] 

I have spent the last 3 hours trying to resolve this, but to no avail. When I call the resource like this:

$scope.komintent = Fakturi.komintenti.get({ id: 1 });

it works perfectly fine, however, I need to pass a dynamic id. This is why I am trying to invoke it like this:

$scope.komintent = Fakturi.komintenti.get({ id: $scope.faktura.KomintentID });

Above this line, I have the following code (both lines together):

$scope.faktura = Fakturi.fakturi.get({ id: $routeParams.id });
$scope.komintent = Fakturi.komintenti.get({ id: $scope.faktura.KomintentID });

Despite trying numerous solutions, I have been unable to make it work.

Interestingly, I have another similar example where everything works as expected: I can use both parameters as $scope.x.y

Fakturi.fakturaKomintent.update({ Fid: $scope.faktura.DokumentID, id: $scope.komintent.KomintentID });

I even tried setting isArray: false, but it didn't help. Here is my web API code: http://prntscr.com/7k6wwt

Even after trying with and without [Route], the issue persists.

Another attempt:

$scope.kom = function () { Fakturi.komintenti.get({ id: $scope.faktura.KomintentID }, function success(data) { $scope.komintent = data }); }

Followed by

input ng-click="kom()"/>

works fine when called, but I prefer it to initialize on page load

It works with $routeParams.id instead of $scope.faktura.KomintentID, but the id is not what I need

Here is a screenshot from the resource factory: http://prntscr.com/7k7bs2

*Another strange observation: When I place the line in a watchGroup, it works perfectly. However, the page flickers when I type something because it keeps refreshing, although it doesn't throw any errors.

Answer №1

If you're making an AJAX call, make sure to initialize the variables within the callback methods:

Fakturi.fakturi.get({ id: $routeParams.id }, function (data) { $scope.faktura = data; });
Fakturi.komintenti.get({ id: $scope.faktura.KomintentID }, function (data) { $scope.komintent = data; });

As mentioned in this source, if you need an immediate response, consider using the query() method for the returned object

$scope.faktura = Fakturi.fakturi.get({ id: $routeParams.id }).query();

Answer №2

Here is the solution that worked for me:

To solve the issue, I incorporated the second ajax call as a function within the success function of the initial ajax call:

Fakturi.fakturi.get({ id: $routeParams.id }, function success(data) { $scope.faktura = data, $scope.komintent = Fakturi.komintenti.get({ id: data.KomintentID }) }, function error(data) { alert("error") });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When dealing with arrays, the parentElement.remove() function may not be defined

Query: e1.parentElement.remove(); is not defined debug: e1 contains a value. e1.remove() removes a button, however, I need to remove a group of buttons. global variable and function execution var x = 0; AllResponses(null, null); primary function featuri ...

Unsure how to proceed with resolving lint errors that are causing changes in the code

Updated. I made changes to the code but I am still encountering the following error: Error Type 'String' is not assignable to type 'string'. 'string' is a primitive, but 'String' is a wrapper object. It is recom ...

How to Remove Multiple Entries With Checkbox Selection in ASP.NET MVC Without Entity Framework

I am currently working on a project with ASP.NET MVC and I am facing an issue while trying to delete multiple records using checkboxes. Whenever I select a record and click on the Delete button, it is taking the Id as null. Can someone please suggest a sol ...

Make window.print() trigger the download of a PDF file

Recently, I added a new feature to my website allowing users to download the site's content by following these instructions: Printing with Javascript || Mozilla Dev. Essentially, the user can print a newly formatted site stored in a hidden iframe. Now ...

Angular promise fails to resolve after an extended period of waiting for a response

My application is designed to send GET requests to my node/express server. In order to monitor changes in the server code, I have implemented a setTimeout function. While the promise function on the client side initially functions properly for a short peri ...

What is the best way to change the class of an input using jQuery when it is not empty?

Utilizing Bootstrap 4. I am working on a feature where I have four text inputs. If any of these inputs contain values, I want to add a specific class to a button. Upon clicking the button, my goal is to clear all input values and remove the aforementione ...

What happens when Google Polymer platform is used without defining _polyfilled?

My attempt at creating a simple example using Google Polymer's platform.js is running into an error message that says: Uncaught TypeError: Cannot read property '_polyfilled' of undefined This is what I'm attempting to achieve: <cur ...

I'm currently learning about things that never change and struggling to grasp their meaning

I'm currently delving into the world of immutable.js record and trying to wrap my head around it. However, this particular piece of code is really throwing me for a loop. Here's my Question: I understand [import, export,const], but what ex ...

Guide to setting up MEAN stack on Cent OS

Hi there, I am a beginner in the world of software development and currently, I am exploring MEAN Stack. However, I am facing difficulties installing it on Cent OS 7. My system specifications are as follows: 1. i7 Processor 2. 12 GB RAM 3. 512 GB Hard Di ...

Dividing a JSON string and incorporating them into individual tds using AngularJS

Currently, I am working on extracting values from a JSON string by splitting it at the ":" and displaying the two values in separate td tags. function testCtrl($scope) { $scope.response = {"name":["The name field is required."],"param":["Hobby: Coding", " ...

JavaScript class name modifications are not functioning as expected

Testing a sprite sheet on a small web page I created. The code for each sprite in sprites.css is structured like this... .a320_0 { top: 0px; left: 0px; width: 60px; height: 64px; background: url("images/sprites.png") no-repeat -787 ...

Array with multiple dimensions using commas as delimiters

My array (array[]) contains elements in the format below, separated by a comma: array[0] = abc, def, 123, ghi I want to transform this array into another multi-dimensional array (arrayTwo[]), structured like this: arrayTwo[0][0] = "abc" arrayTwo[0][1] = ...

Error encountered while compiling a method within a .vue component due to a syntax issue

I have been closely following a tutorial on Vue.js app development from this link. The guide instructed me to add a login() function in the block of the Login.vue file. Here is the snippet of code provided: login() { fb.auth.signInWithEmailAndPa ...

Tips on arranging JSON elements based on the sequence of another JSON

Currently, I am facing a challenge with sorting a list of parks (park_list) based on both distance and area. The code snippet below successfully sorts the list by distance: sortList(index){ return function(a, b){ return (a[index] === b[index] ? 0 : ...

Heroku-hosted application experiencing issues with loading website content

I have been working on a nodejs application that serves a web page using express and also functions as a discord bot. The app runs smoothly on localhost with both the web page and discord functionalities working correctly. However, when I deploy it to Hero ...

Focusing on pinpointing certain mistakes within Drupal 7

When working with Drupal, encountering errors is a common issue. While some errors are simple to fix, others can be quite complex and require significant time and effort to resolve, even if the website appears to function normally despite the error. My qu ...

What's the best way to set up server-side pagination for mui-datatable?

Is server-side pagination for mui-datatable implementation a complex task? Challenges: I am facing difficulties in capturing the user-selected value from the rowsPerPage options. When a user selects '15', how can I update these values within ...

Attempting to include a standard VAT rate of 5% on the invoice

/* The code format is correct and I don't see any issues on my end, I hope it works well for you. */ I need assistance in adding a fixed VAT (tax) rate of 5%. The tax amount should be displayed on the row, while the total tax should reflect the sum o ...

Nodeca's js-yaml now allows appending '>-' to handle extended strings with ease

With the npm package js-yaml, I am attempting to work with a .yaml file. Although I can manipulate it successfully, I encounter an issue when trying to save a long string. Actual: abvs_adas: >- c2Rhc2Rhc2Rhc2Rhc2RwaW9qbGtkZ2hqbGtzZGhmZ2psaGFzamh ...

Dynamically loading iframes with JQuery

I have implemented a jQuery script to load another URL after a successful AJAX request. $(document).ready(function() { var $loaded = $("#siteloader").data('loaded'); if($loaded == false){ $("#siteloader").load(function (){ ...