我有一个设置,其中包含用于获取项目列表的服务、用于对该列表执行某些操作的 Controller 以及用于遍历并显示每个项目的 View 。
事实是,我的项目是指向 RSS 提要的链接,在 Controller 中我想通过这些 RSS 提要进行解析并设置要处理的 View 的模型数据。
现在,需要对建模进行更多修改(我需要对实际的 RSS 提要内容建模),但我的第一个问题是服务获取的数据在我的 Controller 中不可修改(因为调用还没有完成,在我尝试访问它的时候,我猜)。如果我将它写入控制台,基本上它只是一个空数组。
所以我需要知道服务调用完成后如何在 Controller 中触发数据操作。
谢谢!! //乔金
服务代码:
angular.module('itemfeedServices', ['ngResource']).
factory('Item', function($resource){
return $resource('items/:itemId.json', {}, {
query: {method:'GET', params:{itemId:'items'}, isArray:true}
});
});
Controller 代码:
function ItemListCtrl($scope, Item) {
$scope.items = Item.query();
console.log($scope.items); // gives []
}
最佳答案
基本上,我想你想要回调?文档说:
The action methods on the class object or instance object can be invoked with the following parameters:
HTTP GET "class" actions: Resource.action([parameters], [success], [error])
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
non-GET instance actions: instance.$action([parameters], [success], [error])
所以,使用:
Item.query({}, function() {
//you callback
});
应该可以。
关于javascript - Angular.js - 使用服务需要回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659003/