【问题标题】:how to use angular resource sails?如何使用角度资源帆?
【发布时间】:2015-09-02 04:51:43
【问题描述】:

我正在使用角度资源帆。

var items = sailsResource('roles').query(); // GET /item
$scope.roles = items;
angular.forEach($scope.roles, function(value, key) {
    console.log(key + ': ' + value);
});

输出:未定义。

如何解析这个查询?

【问题讨论】:

标签: javascript jquery angularjs sails.js


【解决方案1】:

查看这部分文档:https://github.com/angular-resource-sails/angular-resource-sails#success-and-error-callbacks

如果您想访问您获取的数据,您可能必须为查询函数提供回调。所以你的代码会变成

sailsResource('roles').query(function(items) { // GET /item
    $scope.roles = items;
    angular.forEach($scope.roles, function(value, key) {
        console.log(key + ': ' + value);
    });
});

【讨论】:

    【解决方案2】:

    query 方法是异步的。 sailsResource 创建 $resource API 兼容服务,因此您必须在回调函数中进行循环。

    例如

    $scope.roles = sailsResource('roles').query(function(roles) {
        angular.forEach(roles, function(value, key) {
            // and so on
        });
    });
    

    你也可以使用$promise属性来访问promise,例如

    $scope.roles = sailsResource('roles').query();
    
    $scope.roles.$promise.then(function() {
        angular.forEach($scope.roles, function(value, key) {
            // etc
        });
    });
    

    【讨论】:

    • 感谢您的回复.. 我用过这个
    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 2014-12-09
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多