Angular.js服务功能未定义

时间:2014-09-05 14:29:58

标签: javascript angularjs

我有控制器和服务(我将发布所有控制器和服务代码,因为我不知道可能出现什么问题):

控制器:

'use strict';
app.controller('membersController', ['$scope', 'membersService', function($scope, membersService) {
    $scope.members = [];
    $scope.updatedMembers = [];
    membersService.getMembers().then(function (results) 
    {
        $scope.members =results.data;
    },
    function(error) {
        alert(error.data.message);
    });
    $scope.update = function () {
        membersService.updateMembers($scope.updatedMembers).then(function(results) {
            alert(results);
        },
        function(results) {
            alert(results);
        });
    };
    $scope.updateActive = function(member) {
        if ( !isInArray($scope.updatedMembers,member))
        {
            $scope.updatedMembers.push(member);
        }  
    };
    var isInArray = function(array, item) {
        var found = false;
        for (var i = 0; i < array.length; i++) {
            if (array[i].id == item.id) {
                found = true;
                break;
            }
        }
        return found;
    };
}]);

服务:

'use strict';
app.factory('membersService', ['$http', 'ngAuthSettings', function ($http, ngAuthSettings) {
     var serviceBase = ngAuthSettings.apiServiceBaseUri;
    var membersServiceFactory = {};
    var _getMembers = function () {
         return $http.get(serviceBase + 'api/members').then(function (results) {
            return results;
        });
    };
var _updateMembers = function(updatedMembers) {
        $http.post(serviceBase + 'api/Members/UpdateMembers', updatedMembers, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(
            function (results) {
                return results;
            },
            function(results) {
                return results;
            });
    };
    membersServiceFactory.getMembers = _getMembers;
    membersServiceFactory.updateMembers = _updateMembers;
    return membersServiceFactory;
}]);

这是我在萤火虫中得到的错误:

Error: 
membersService.updateMembers(...) is undefined 
$scope.update@http://localhost:37272/Controllers/membersController.js:16:13
$a.prototype.functionCall/<@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:172:370 
fc[c]</<.compile/</</<@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:189:395
Yd/this.$get</h.prototype.$eval@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:108:471
Yd/this.$get</h.prototype.$apply@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:109:230 
fc[c]</<.compile/</<@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:189:370
ne/c/<@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:31:30
q@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:7:363
ne/c@http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:31:14

任何人都可以将我指向正确的方向,因为我是angular.js的新手。另外我想注意,即使出现此错误,请求也会传递给.net webapi

1 个答案:

答案 0 :(得分:4)

如果在控制器中你想像承诺对象一样使用它,我的意思是:

  $scope.update = function () {
        membersService.updateMembers($scope.updatedMembers).then(function(results) {
            alert(results);
        },
        function(results) {
            alert(results);
        });
    };

然后你应该返回promise对象,$http get方法本身正在返回promise。

'use strict';
app.factory('membersService', ['$http', 'ngAuthSettings', function ($http, ngAuthSettings) {
     var serviceBase = ngAuthSettings.apiServiceBaseUri;
    var membersServiceFactory = {};
    var _getMembers = function () {
         return $http.get(serviceBase + 'api/members');
    };
var _updateMembers = function(updatedMembers) {
        $http.post(serviceBase + 'api/Members/UpdateMembers', updatedMembers, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });;
    };
    membersServiceFactory.getMembers = _getMembers;
    membersServiceFactory.updateMembers = _updateMembers;
    return membersServiceFactory;
}]);