有没有一种方法,我可以返回从$资源的承诺,而不需要使用$ Q?而不、方法、资源

2023-09-13 03:29:03 作者:青春如此疯狂,

我有codeD以下服务:

I have coded the following service:

angular.module('App')
    .factory('TestService', ['$http', '$q', '$resource', function (
                          $http, $q, $resource) {

        var TestResource = $resource('/api/Tests', {}, {
            saveData: { method: 'PUT' },
            deleteData: { method: "DELETE", params: { TestId: 0 } }
        });

        var factory = {

            query: function (selectedSubject) {
                var deferred = $q.defer();
                TestResource.query({ subjectId: selectedSubject },
                   function (resp) {
                       deferred.resolve(resp);
                   }
                );
                return deferred.promise;

                //return Test.query({ subjectId: selectedSubject });
            }

        }
        return factory;
}]);

在我的控制器我打电话这样说:

In my controller I am calling it this way:

TestService.query($scope.selectedSubject)
                        .then(function (result) {
                            $scope.gridData = result;
                        }, function (result) {
                            alert("Error: No data returned");
                        });

有没有办法,我可以在我的服务没有$ Q切出code几行的方式。有没有办法,我就回来从$资源的承诺的一种方式?我曾尝试注释掉code,但那个给了我一个错误说没有。那么的方法。

Is there a way that I could cut out a few lines of code by not having $q in my service. Is there a way that I could return a promise from the $resource? I have tried the commented out code but that gives me an error saying there is no ".then" method.

推荐答案

$资源的角度(1.0.8)的当前稳定版本不能返回一个承诺,但它看起来像它会来的1.2版本。 它在V1.1.3新增(V 1.1是不稳定分支):

$resource can't return a promise in the current stable version of Angular (1.0.8), but it looks like it'll come in version 1.2. It was added in v1.1.3 (v 1.1 is the unstable branch):

$资源:通过$揭露基于API的承诺和再解决$(的 dba6bc73 )

$resource: expose promise based api via $then and $resolved (dba6bc73)