创建新的 angular $资源
Creating a new angular $resource
我有一个页面用于在我们的 restful 网络服务中创建新资源。页面控制器如下:
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();
};
}]);
我的服务 Person 看起来像这样:
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>
原因是,正如经常发生的那样,不是您要找的地方。问题出在这里:
appController.controller('AddCtrl', ['$scope', 'Person',
function($scope, $location, Person) {
您已经为您的控制器指定了两个依赖项,但是您的函数有 3 个参数 - 最后一个参数为空并且您的 Person 服务被指定为 $location
。应该是:
appController.controller('AddCtrl', ['$scope', '$location', 'Person',
function($scope, $location, Person) {
假设您使用的是 $location。
您的资源中会出现完全相同的问题 - uuid 始终为空。
我有一个页面用于在我们的 restful 网络服务中创建新资源。页面控制器如下:
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();
};
}]);
我的服务 Person 看起来像这样:
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>
原因是,正如经常发生的那样,不是您要找的地方。问题出在这里:
appController.controller('AddCtrl', ['$scope', 'Person',
function($scope, $location, Person) {
您已经为您的控制器指定了两个依赖项,但是您的函数有 3 个参数 - 最后一个参数为空并且您的 Person 服务被指定为 $location
。应该是:
appController.controller('AddCtrl', ['$scope', '$location', 'Person',
function($scope, $location, Person) {
假设您使用的是 $location。
您的资源中会出现完全相同的问题 - uuid 始终为空。