Angularjs resource with scope parameter

83 Views Asked by At

My problem is the following: I am trying to call resource with the following parameter and I get the following error:

[$resource:badcfg] 

I tried fixing this in the past 3 hours and I cant seem to make it work. So, if i call it like this:

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

it works properly and everything is fine, but since i need dynamic id there, I am trying to invoke it with

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

ABOVE this line I have this (both lines together):

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

I tried every possible solution and cant make it work.

I have another similar example where everything works: I can put both parameters as $scope.x.y

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

I changed to isArray: false, still nothing. Here is my web api code: http://prntscr.com/7k6wwt

I changed it with and without [Route], still nothing.

When i try like this:

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

and than I assign

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

it works fine, but I want it to initialize on page load

with $routeParams.id instead of $scope.faktura.KomintentID it works but its not the id i need

A screenshot from the resource factory: http://prntscr.com/7k7bs2

*Also a very weird thing: When i put the line in watchGroup it works perfectly, but when I type something the page is flickering because its refreshing all the time, but it works fine and does not throw errors

2

There are 2 best solutions below

1
On BEST ANSWER

If this is an AJAX call then the varialble initialization should be into 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; });

According to this link, if you would like to get response immediately then try to invoke query() method for the returned object

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

The answer was this:

I had to use the second ajax call as a function in the success function of the first 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") });