简体   繁体   English

具有范围参数的AngularJS资源

[英]Angularjs resource with scope parameter

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. 我在过去3个小时内尝试解决此问题,但似乎无法使其正常工作。 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 它可以正常工作,并且一切都很好,但是由于我在那里需要动态id,因此我尝试使用

$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.xy 我还有另一个类似的示例,其中的所有操作均有效:我可以将两个参数都设置为$ scope.xy

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

I changed to isArray: false, still nothing. 我更改为isArray:false,仍然没有。 Here is my web api code: http://prntscr.com/7k6wwt 这是我的网络api代码: http : //prntscr.com/7k6wwt

I changed it with and without [Route] , still nothing. 我在有[Route]和没有[Route]情况下进行了更改,还是一无所获。

When i try like this: 当我这样尝试时:

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

and than I assign 然后我分配

input ng-click="kom()"/> 输入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 用$ routeParams.id代替$ scope.faktura.KomintentID可以工作,但不是我需要的ID

A screenshot from the resource factory: http://prntscr.com/7k7bs2 资源工厂的屏幕截图: 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 *还有一件很奇怪的事情:当我将行放在watchGroup中时,它工作正常,但是当我键入内容时,页面一直闪烁,因为它一直在刷新,但工作正常,不会引发错误

If this is an AJAX call then the varialble initialization should be into the callback methods: 如果这是AJAX调用,则应将可变初始化放入回调方法中:

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 根据此链接 ,如果您想立即获得响应,则尝试为返回的对象调用query()方法

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

The answer was this: 答案是这样的:

I had to use the second ajax call as a function in the success function of the first ajax call: 在第一个ajax调用的成功函数中,我不得不将第二个ajax调用用作函数:

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") }); Fakturi.fakturi.get({id:$ routeParams.id},函数success(data){$ scope.faktura = data,$ scope.komintent = Fakturi.komintenti.get({id:data.KomintentID})},函数错误(数据){警报(“错误”)});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM