我有一個工廠,做一個http調用,並在錯誤回調我想設置一個屬性值,例如this.password = null
但this.password
不指向密碼。在AngularJS中如何修改從服務內部的回調服務屬性
在另一方面,當我做POST數據發送罰款:password: this.password
密碼財產的範圍應該是不同的吧?或者我錯過了別的東西?
.factory('LogInService', function($http) {
return {
username: null,
password: null,
logIn: function() {
return $http({
method: 'POST',
url: 'http://my-domain/api/signin',
data: {
username: this.username,
password: this.password
}
})
.then(
function(success){
},
function(err){
// reset password field
this.password = null; // <-- not working
}
);
}
}
});
我可以將所有屬性和方法設置爲一個var然後返回它,但我寧願上面的方法。我的意思是:
var myService = {
// my property and methods...
}
errorCallBack = function(){
myService.password = null;
}
return myService;
我正在嘗試'var _this = this;'但在'return'之前,在'logIn'函數內工作。合理。謝謝 – Mikel