AngularJS工厂未定义错误

时间:2014-09-06 08:03:12

标签: angularjs

我被困在这里,我不知道我错过了什么,或者如何进一步调试。我继续收到此错误:当我从ng-click事件中调用它时,'updateMemberServiceFactory未定义'。请指教。如果这是一个简单的错字我道歉我只是看不出有什么不对。我试图在我的控制器上调用PUT方法,但它永远不会被调用。 AngularJS新手。谢谢。

securityApp.factory('updateMemberServiceFactory', function ($http) {

    function update(memberServiceID) {

        $http({ method: 'PUT', url: 'http://localhost:62791/api/MemberServices/', data: { memberServiceID: memberServiceID } })
            .then(function (result) {
                alert('success');
            }, function (errorResponse) {

        });
    };

});

securityApp.controller('memberServicesController', function ($scope, $http, $routeParams, $location, getTokenFromServer, updateMemberServiceFactory) {

    var id = $routeParams.memberID;

    $scope.username = 'aharris1@test.com';
    $scope.password = 'SuperPass1!';

    getTokenFromServer.getToken($scope.username, $scope.password).then(function (data) {
        $scope.token = data;

        $http({ method: 'GET', url: '/api/MemberServices/' + id + '?access_token=' + $scope.token, headers: { 'Authorization': 'Bearer ' + $scope.token } })
            .success(function (response) {

                $scope.memberServices = "";
                $scope.memberServices = response;

                $http({ method: 'GET', url: '/api/Members/' + id + '?access_token=' + $scope.token, headers: { 'Authorization': 'Bearer ' + $scope.token } })
                    .success(function (response) {
                        $scope.member = response;
                    });

                $http.get('/api/ServiceTypes/')
                    .then(function (response) {
                        $scope.serviceTypes = response.data;
                    });
            });
    });

    $scope.updateMemberService = function () {        
        updateMemberServiceFactory.update( { memberServiceID: memberServiceID }, null, function () {
            alert('update called');
        });
    };

});

<a href="" style="text-decoration: none;" ng-click="updateMemberService(memberService.memberServiceID)"><i class="fa fa-save"></i></a>  

1 个答案:

答案 0 :(得分:3)

当您使用someApp.factory(someFunction)时,某些someFunction应返回将在需要时注入的对象。

在你的情况下:

securityApp.factory('updateMemberServiceFactory', function ($http) {

    function update(memberServiceID) {

        $http({ method: 'PUT', url: 'http://localhost:62791/api/MemberServices/', data: { memberServiceID: memberServiceID } })
            .then(function (result) {
                alert('success');
            }, function (errorResponse) {

        });
    };

    return { // <---- this object will get injected, when required
         update : update
    }

});