Angular seavice getAll不是一个函数

时间:2015-10-03 12:09:46

标签: angularjs rest

我在Angular控制器中有一个功能,如下所示:

(function () {
    'use strict';
    var controlledID = 'Clause.Clause';
    angular.module('docapp').controller(controlledID, ['$scope', 'common', 'taskSvc', 'clauseSvc', Clauses]);

    function Clauses($scope, clauseSvc, taskSvc, common) {
        $scope.GetContractMaster = function GetContractMaster() {
            clauseSvc.getAll()
            .then(function (response) {
                $scope.ContractMaster.rowData = response.d.results;
                console.log(response.d.results);
            });
        };
        $scope.GetContractMaster();
   };
}
})();

我的服务

(function () {
    'use strict';
    var app = angular.module('docapp');
    app.factory("clauseSvc", ["baseSvc", function (baseService) {
        var listEndPoint = '/_api/web/lists/';
        var getAll = function () {
            var query = listEndPoint + "GetByTitle('CLAUSE_MST')/Items?$select=Title,Title,Desc,nodes/ID&$expand=nodes";
            return baseService.getRequest(query);
        };
        return {
            getAll: getAll
        };
    }]);
})();

baseService

"use strict";
(function () {
    angular.module("docapp")
        .factory("baseSvc", ["$http", "$q", function ($http, $q) {
            var baseUrl = _spPageContextInfo.siteAbsoluteUrl;
            var getRequest = function (query) {
                var deferred = $q.defer();
                $http({
                    url: baseUrl + query,
                    method: "GET",
                    headers: {
                        "accept": "application/json;odata=verbose",
                        "content-Type": "application/json;odata=verbose"
                    }
                })
                    .success(function (result) {
                        deferred.resolve(result);
                    })
                    .error(function (result, status) {
                        deferred.reject(status);
                    });
                return deferred.promise;
            };

            return {
                getRequest: getRequest

            };
        }]);
})();

获取错误

[true]  [SYSERR] clauseSvc.getAll is not a function Object {exception:
TypeError: clauseSvc.getAll is not a function
at n.GetContractMaster

需要帮助

1 个答案:

答案 0 :(得分:3)

您的控制器声明为

    .controller('$scope', 'common',  'taskSvc', 'clauseSvc', Clauses]);

并将该函数声明为

function Clauses($scope,   clauseSvc, taskSvc,  common)

因此,变量clauseSvc实际上是common服务实例,变量common实际上是clauseSvc服务实例。

帮自己一个忙:避免这种错误,只需避免使用此数组语法,并使用ngAnnotate使代码可以缩小,从而使代码更容易编写和读取。