2014-09-06 86 views
0

我被困在這裏,我不知道我缺少什麼或如何進一步調試。我繼續得到這個錯誤:當我從ng-click事件中調用它時'updateMemberServiceFactory未定義'。請指教。如果這是一個簡單的錯字,我道歉,我只是不明白什麼是錯的。我試圖調用我的控制器上的PUT方法,但它永遠不會被調用。 AngularJS新手。謝謝。AngularJS factory undefined error

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 = '[email protected]'; 
    $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> 
+0

「錯誤:updateMemberServiceFactory是未定義 [email protected]://本地主機:62791 /腳本/應用/ Security.js:357:9 – 2014-09-06 08:04:39

回答

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 
    } 

}); 
+0

非常感謝你! – 2014-09-06 23:28:31