Ang*_*ngu 8 javascript jquery angularjs sails.js
我正在使用角度资源风帆.
var items = sailsResource('roles').query(); // GET /item
$scope.roles = items;
angular.forEach($scope.roles, function(value, key) {
console.log(key + ': ' + value);
});
Run Code Online (Sandbox Code Playgroud)
输出:未定义.
如何解析这个查询?
该query方法是异步的。sailsResource创建$resourceAPI 兼容服务,因此您必须在回调函数中进行循环。
例如
$scope.roles = sailsResource('roles').query(function(roles) {
angular.forEach(roles, function(value, key) {
// and so on
});
});
Run Code Online (Sandbox Code Playgroud)
您还可以使用该$promise属性来访问承诺,例如
$scope.roles = sailsResource('roles').query();
$scope.roles.$promise.then(function() {
angular.forEach($scope.roles, function(value, key) {
// etc
});
});
Run Code Online (Sandbox Code Playgroud)