[英]Cannot read property of undefined. How to define?
I am trying to run a custom PUT in Angularjs, however I cannot for the life of me get it to work. 我试图在Angularjs中运行自定义PUT,但是我终生无法正常工作。 I need both UserResource and user to be defined but only the parameter in the first position of the update function gets defined.
我需要同时定义UserResource和user,但是仅定义了update函数第一位置的参数。 What can I do to define both user and UserResource?
如何定义用户和UserResource?
(function () {
class AdminController {
constructor(User, Auth) {
this.users = User.query();
this.Auth = Auth;
}
//here user is defined and I can run the delete function
delete(user) {
user.$remove();
this.users.splice(this.users.indexOf(user), 1);
}
// and if I put user into the first position it is defined but UserResource isn't
// as written here it isn't defined either.
update(UserResource, user) {
var $id = user._id;
UserResource.update({ id:$id }, user);
}
}
angular.module('wcsdesktopApp.admin')
.controller('AdminController', AdminController);
})();
EDIT to add more information 编辑以添加更多信息
I call the update function like this: 我这样调用更新函数:
<table>
<tr>
<a ng-click="admin.update(user)" class="update"><span class="fa fa-refresh fa-2x"></span></a>
</tr>
<tr>
<a ng-click="admin.delete(user)" class="trash"><span class="fa fa-trash fa-2x"></span></a>
</tr>
</table>
I included the delete function for reference since that is working. 我包括删除功能以供参考,因为它可以正常工作。
UserResource is an AngularJS $resource custom put request. UserResource是AngularJS $ resource自定义放置请求。
(function() {
function UserResource($resource) {
return $resource('/api/users/:id/:controller', {
id: '@_id'
}, {
changePassword: {
method: 'PUT',
params: {
controller: 'password'
}
},
update: {
method: 'PUT'
},
get: {
method: 'GET',
params: {
id: 'me'
}
}
});
}
angular.module('wcsdesktopApp.auth')
.factory('User', UserResource);
})();
You need to make sure that you are injecting your UserResource
service into your controller. 您需要确保将
UserResource
服务注入到控制器中。 One of the many ways would be like the below: 许多方式之一如下所示:
angular.module('wcsdesktopApp.admin').controller('AdminController', AdminController);
AdminController.$inject = [ 'User'];
function AdminController(User) {}
Doing that would then change your update
signature in your controller to the following: 然后,将控制器中的
update
签名更改为以下内容:
update(user) {
User.update(); // Do whatever update.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.