0

我正在尝试一些建议给我的代码,但它似乎给 $q 一个错误。这是我的示例代码:

angular.module('common')
    .factory('gridService',
    [
        '$http',
        '$q',
        '$resource',
        '$timeout',
        'localStorageService',
        'optionService',
        'utilityService',
    function (
        $http,
        $resource,
        $q,
        $timeout,
        localStorageService,
        optionService,
        utilityService) {

        var factory = {

            init: function ($scope) {

                $scope.saveData = function () {

                    var entityIdColumn = $scope.entityType.toLowerCase() + 'Id';
                    var requests = $scope.grid.data
                      .filter(function (rowData, i) {
                          return !angular.equals(rowData, $scope.grid.backup[i]);
                      })
                      .map(function (rowData, i) {
                          var entityId = rowData[entityIdColumn];
                          return $http.put('/api/' + $scope.entityType + '/' + entityId, rowData);
                      });
                    $q.all(requests).then(function (allResponses) {
                        //if all the requests succeeded, this will be called, and $q.all will get an
                        //array of all their responses.
                        console.log(allResponses[0].data);
                    }, function (error) {
                        //This will be called if $q.all finds any of the requests erroring.
                        var abc = error;
                        var def = 99;
                    });

            },

        }

        return factory;

    }]);

错误在这里:

TypeError: Object function t(e, k, f) {
            function q(b, c) { var d = {}; c = u({}, k, c); r(c, function (a, c) { s(a) && (a = a()); var m; a && a.charAt && "@" == a.charAt(0) ? (m = a.substr(1), m = y(m)(b)) : m = a; d[c] = m }); return d } function d(b) { return b.resource } function g(b) { z(b || {}, this) } var F = new n(e); f = u({}, G, f); r(f, function (b, c) {
                var A =
                /^(POST|PUT|PATCH)$/i.test(b.method); g[c] = function (a, c, m, k) {
                    var p = {}, e, f, v; switch (arguments.length) { case 4: v = k, f = m; case 3: case 2: if (s(c)) { if (s(a)) { f = a; v = c; break } f = c; v = m } else { p = a; e = c; f = m; break } case 1: s(a) ? f = a : A ? e = a : p = a; break; case 0: break; default: throw x("badargs", arguments.length); } var n = e instanceof g, l = n ? e : b.isArray ? [] : new g(e), w = {}, t = b.interceptor && b.interceptor.response || d, y = b.interceptor && b.interceptor.responseError || C; r(b, function (a, c) { "params" != c && ("isArray" != c && "interceptor" != c) && (w[c] = z(a)) });
                    A && (w.data = e); F.setUrlParams(w, u({}, q(e, b.params || {}), p), b.url); p = D(w).then(function (c) { var a = c.data, d = l.$promise; if (a) { if (h.isArray(a) !== !!b.isArray) throw x("badcfg", b.isArray ? "array" : "object", h.isArray(a) ? "array" : "object"); b.isArray ? (l.length = 0, r(a, function (a) { l.push(new g(a)) })) : (z(a, l), l.$promise = d) } l.$resolved = !0; c.resource = l; return c }, function (a) { l.$resolved = !0; (v || B)(a); return E.reject(a) }); p = p.then(function (a) { var c = t(a); (f || B)(c, a.headers); return c }, y); return n ? p : (l.$promise = p, l.$resolved =
                    !1, l)
                }; g.prototype["$" + c] = function (a, b, d) { s(a) && (d = b, b = a, a = {}); a = g[c](a, this, b, d); return a.$promise || a }
            }); g.bind = function (b) { return t(e, u({}, k, b), f) }; return g
        } has no method 'all'
    at g.factory.init.$scope.saveData (http://127.0.0.1:82/Content/app/common/services/gridService.js:192:24)

它指向 $q.all 方法。我正在使用最新版本的 AngularJS 1.2

4

1 回答 1

2

$q 和 $resource 参数的顺序不同:

angular.module('common')
.factory('gridService',
[
    '$http',
    '$q',
    '$resource',
    ...
],
function (
    $http,
    $resource,
    $q,
    ...
 ) {

请注意,如果您使用 grunt,则有一个ng-min 任务来处理角度 DI 注释。

于 2013-11-12T15:21:54.550 回答