Angularjs承诺$ resource

时间:2014-08-28 14:46:30

标签: javascript angularjs angular-promise

我正在尝试在angularjs资源中实现promise。但它显示TypeError: Cannot read property 'then' of undefined错误。 AngularJs版本:v1.2.2

我所做的是

控制器

LibrariesFactory.getLibraryId({
    "communityId": $scope.communityId
}).$promise.then(function(successResponse) {
    console.log(successResponse);
}, function(errorResponse) {

});

服务

serviceApp.factory('LibrariesFactory', function($resource, AppSettings) {
    return $resource(AppSettings.stub_url + 'v1/communities/:communityId/libraries', {
        communityId: '@communityId'
    }, {
        getLibraryId: {
            method: 'GET',
            isArray: true
        }
    });
});

任何想法???

1 个答案:

答案 0 :(得分:0)

我得到了这个问题的解决方案。我发布了整个代码供将来参考。

<强>控制器

LibrariesFactory.getLibraryId($scope.communityId).then(function(successResponse) {
    console.log(successResponse);
}, function(errorResponse) {
    // console.log(errorResponse);
});

<强>服务

serviceApp.factory('LibrariesFactory', function($resource, AppSettings, $q) {

    return {

        getLibraryId: function(community_id) {
            console.log(t1);
            var Communities = $resource(AppSettings.stub_url + 'v1/communities/' + community_id + '/libraries', {});
            var deferred = $q.defer();
            var communityObj = new Communities();
            communityObj.$query(null, function(community) {
                console.log(community);
                deferred.resolve(community);
            });
            return deferred.promise;
        }
    };
});

感谢大家的回复。