Javascript 默认的资源操作集以角度返回什么?如何修改返回的资产?
下面我给出了一个示例代码,我想向users数组添加一个元素。当我控制台记录用户对象时,我得到一个包含1个元素的数组,但它有3个资源对象、一个promise和一个$resolved属性。有人能解释一下我遗漏了什么吗Javascript 默认的资源操作集以角度返回什么?如何修改返回的资产?,javascript,angularjs,Javascript,Angularjs,下面我给出了一个示例代码,我想向users数组添加一个元素。当我控制台记录用户对象时,我得到一个包含1个元素的数组,但它有3个资源对象、一个promise和一个$resolved属性。有人能解释一下我遗漏了什么吗 /*@ngInject*/ constructor(User, Auth, $state, $uibModal, $scope) { this.Auth = Auth; this.$state = $state; this.$uibModa
/*@ngInject*/
constructor(User, Auth, $state, $uibModal, $scope) {
this.Auth = Auth;
this.$state = $state;
this.$uibModal = $uibModal;
this.$scope = $scope;
this.newUser = {name: "New User", email: "newUser@gmail.com", password: "gg"};
this.users = User.query();
this.users.push(this.newUser);
console.log(this.users);
this.$scope.$watch('this.user', function(newUser) {
console.log("entered watch");
var val = this.users.indexOf(newUser);
if(val == this.users.length - 1 ) {
this.openNewUser();
}
});
}
您唯一缺少的是对
User.query()
的调用是异步的,这意味着返回值将在以后填充
如果要执行依赖于此.users的代码,则需要将回调附加到查询的承诺(this.users.$promise
)
例如:
//First get the users
var self = this;
this.users = User.query();
this.users.$promise.then(function(){
//Do stuff with the user list
self.users.push(this.newUser);
});
如您所示,我试图在success函数中执行此操作,但它表示this.users.push()中的“this”未定义。这里发生了什么?我如何修复它?实际上我希望它出现在es6中,但我用你以前的方法解决了范围问题。我正在尝试修复它,但我不认为我理解范围的问题。