2015-09-22 105 views
0

我有一個頁面用於在我們寧靜的web服務中創建一個新的資源。頁面的控制器如下:

appController.controller('AddCtrl', ['$scope', 'Person', 
     function($scope, $location, Person) { 
    $scope.person = new Person(); 
    $scope.emailList = []; 
    $scope.add = function() { 
     $scope.emailList.push({email: $scope.email}); 
     $scope.person.emailList = $scope.emailList; 
     $scope.person.$save(); 
    }; 
}]); 

我的服務,人,看起來是這樣的:

appService.factory('Person', ['$resource', 
    function($resource, uuid){ 
     return $resource(baseUrl + 'candidate/:uuid', {}, { 
      query: {method:'GET', isArray:true}, 
      get: {method:'GET', params:{uuid: uuid}}, 
      save: {method: 'POST'}, 
      remove: {method: 'DELETE', params:{uuid: uuid}} 
     }); 
    } 
]); 

然而,當我查看該頁面,我注意到在該行的錯誤:

$scope.person = new Person(); 

Person is not a function at new <anonymous> ... 

我不知道爲什麼這是一個問題。之前它工作正常。我也嘗試添加一個方法創建的服務:

appService.factory('Person', ['$resource', 
    function($resource, uuid){ 
     var r = $resource(baseUrl + 'candidate/:uuid', {}, { 
      ... 
     }); 
     r.create = function() { 
      return $resource(baseUrl + 'candidate', {}); 
     } 
     return r; 
    } 
]); 

不過,我收到我的控制器以下錯誤:

$scope.person = Person.create(); 

TypeError: Cannot read property 'create' of undefined at new <anonymous> 

回答

2

的原因是,因爲它經常發生,不是你正在尋找它。問題坐落在這裏:

appController.controller('AddCtrl', ['$scope', 'Person', 
    function($scope, $location, Person) { 

您指定兩個依賴於你的控制器,但你的函數有3個參數 - 最後一個是空的,你的個人服務得到了指定爲$location。它應該是:

appController.controller('AddCtrl', ['$scope', '$location', 'Person', 
    function($scope, $location, Person) { 

假設您使用的是$ location。

您的資源中將會有完全相同的問題 - uuid始終爲空。