【问题标题】:AngularJs $resource - "then" function not being calledAngularJs $resource - 没有调用“then”函数
【发布时间】:2016-12-11 14:50:57
【问题描述】:

Controller.js:

service.getUsers(params).$promise.then(function(response) {
    var data = response;
    $scope.users = data.resources;
    $scope.totalItems = data.totalResults;
});

Service.js:

return $resource('/Users', {}, {
            getUsers: {
                method: 'GET',
                headers: {'segment' : 'computedSegment'},
                isArray: true,
                transformResponse: function(data, header){
                    var wrapped = angular.fromJson(data);
                    angular.forEach(wrapped.resources, function(item, idx) {

                        wrapped.resources[idx] = item;
                    });

                    var deffered = $q.defer();
                    deffered.resolve(wrapped);



                    return deffered.promise;
                }
            }});  

我遇到的问题是,虽然请求发送到服务器,但我收到了响应,数据被正确处理,永远不会执行 ,,then" 函数来设置 $scope 变量。

有人有想法吗?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    我猜你忘了在 getUsers 方法之后使用 query() 方法。而且我认为您不需要在 transformResponse 方法中使用 promise。你可以查看这个answers

    【讨论】:

    • 你说的查询方法到底应该用在哪里?
    • 喜欢; service.getUsers(params).query().$promise
    • TypeError: UsersService.getUsers(...).query is not a function 是我将代码更改为后收到的:UsersService.getUsers(params).query().$promise.then (功能(响应)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多