无法读取未定义的 属性。如何定义?

Cannot read property of undefined. How to define?

我正在尝试 运行 在 Angularjs 中自定义 PUT,但我终究无法让它工作。我需要同时定义 UserResource 和 user,但只定义更新函数第一个位置的参数。我可以做些什么来定义用户和 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);
})();

编辑以添加更多信息

我这样调用更新函数:

  <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>

我包含了删除功能以供参考,因为它可以正常工作。

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);

})();

您需要确保将 UserResource 服务注入控制器。许多方法之一如下所示:

angular.module('wcsdesktopApp.admin').controller('AdminController', AdminController);
AdminController.$inject = [ 'User'];
function AdminController(User) {}

这样做会将您在控制器中的 update 签名更改为以下内容:

update(user) {
    User.update(); // Do whatever update.
}