假设我有一个名为“Stuff'。
的工厂。”app.factory('Stuff', function($resource) {
return $resource('api/v1/stuff/', {}, {
getThings: {
method: 'GET',
params: {id: '@id'},
url: 'api/v1/things/:id',
isArray: true
}
});
});
在这个工厂里,我有一个自定义的getThings函数。
在我的控制器中,如果我这样做:
$scope.stuffs = Stuff.query();
$scope.things = Stuff.getThings({id: $scope.stuffs[0].id});
它有效,$ scope.things就是我想要的。
但如果我这样做:
$scope.stuffs = Stuff.query();
$scope.things = $scope.stuffs[0].$getThings();
我在角度资源中遇到错误。 ajax调用发生在正确的url上,我得到正确的数据,但getThings在获取数据后立即出错,而$ scope.things中唯一剩下的就是看似承诺:
Object {then: function, catch: function, finally: function}
JS控制台中的错误是:
TypeError: undefined is not a function
at http://dev.blah.com/assets/angular-resource/angular-resource.js?body=1:558:27
at forEach (http://dev.blah.com/assets/angular/angular.js?body=1:326:18)
at $http.then.value.$resolved (http://dev.blah.com/assets/angular-resource/angular-resource.js?body=1:556:17)
at wrappedCallback (http://dev.blah.com/assets/angular/angular.js?body=1:11574:81)
at wrappedCallback (http://dev.blah.com/assets/angular/angular.js?body=1:11574:81)
at http://dev.blah.com/assets/angular/angular.js?body=1:11660:26
at Scope.$eval (http://dev.blah.com/assets/angular/angular.js?body=1:12703:28)
at Scope.$digest (http://dev.blah.com/assets/angular/angular.js?body=1:12515:31)
at Scope.$apply (http://dev.blah.com/assets/angular/angular.js?body=1:12807:24)
at done (http://dev.blah.com/assets/angular/angular.js?body=1:8380:45)
我无法弄清楚为什么这不起作用。
答案 0 :(得分:0)
我相信在资源查询中返回一个promise。试试这个:
Stuff.query(function(response) {
response[0].$getThings(function(things) {
$scope.things = things;
);
});
或者:
Stuff.query()
.$promise
.then(function(stuff) {
$scope.stuffs = stuff;
return stuff[0].$getThings();
})
.then(function(things) {
$scope.things = things;
});
答案 1 :(得分:0)
请参阅此plnkr http://plnkr.co/edit/qlMrZ9?p=preview。您在$ resource.query()上返回$ promise并在解析之前使用返回的值将导致错误。
这是在services.js
中var myServices = angular.module('myApp.services', ['ngResource']);
myServices.factory('Stuff', ['$resource', function($resource) {
return $resource('stuff.json', {}, {
getThings: {
method: 'GET',
params: {id: '@id'},
url: ':id/things.json',
isArray: false
}
});
}]);
这是在controllers.js
中angular.module('myApp.controllers', [])
.controller('MyCtrl1', ['$scope', 'Stuff', function($scope, Stuff) {
$scope.data = {};
$scope.thingies = [];
$scope.data = Stuff.get();
console.log('is stuffs resolved='+$scope.data.$resolved);
$scope.thingies = Stuff.getThings({id: 'id1'});
console.log('is thingies resolved='+$scope.thingies.$resolved);
}])
.controller('MyCtrl2', ['$scope', 'Stuff', function($scope, Stuff) {
$scope.data = {};
$scope.thingies = [];
$scope.status = [];
Stuff.get(function(response){}).$promise.then(
function(value1) {
$scope.data = value1;
console.log("MyCtrl 2 stuffs="+$scope.data.stuffs[0].id);
Stuff.getThings({id:$scope.data.stuffs[0].id}).
$promise.then(
function(value2){
$scope.thingies = value2;
console.log("MyCtrl 2 thingies="+value2.things);
}, function(error2){
console.log('error in Stuff.getThingies='+error2);
$scope.status = error2;
});
}, function(error1){
console.log('error in Stuff.get='+error1);
$scope.status = error1;
}
);
}]);
我不了解您的代码行。这条线路无法使用。
$scope.stuffs[0].$getThings();
您需要向我们展示您的实际代码或创建一个plnkr,以便我们可以更好地帮助您。