-1
目前我的控制器如下所示。我必須創建一個新變量parent
來存儲控制器的上下文,以便我可以在登錄函數返回的許諾內使用它。登錄功能在另一個服務中。Angularjs在promise中使用「this」關鍵字回電
是否有使用this
關鍵字來引用響應對象而不是使用變量並在其中存儲當前上下文?登錄功能是通過ng-click事件調用的。
function loginCtrl(Service, $state) {
this.emailAddress =null;
this.password = null;
var parent = this;
parent.response = common.getNewResponseObject();
this.login = function() {
Service.login(this.emailAddress, this.password)
.success(success)
.error(error);
}
function error(data) {
parent.response.errorResponse = { errors: [{ message: data.error_description }] }
}
function success(data) {
authService.setAuthData(data);
$state.go(common.states.dashboard);
}
}
angular
.module('app')
.controller('loginCtrl', ['Service', '$state', loginCtrl])
將vm附加到此使用Var vm =此 –
,沒有任何區別。我希望能夠在promise回調中做到這一點。 – joe
開始時賦值變量,不要再次使用'this'來保持一致性。請參閱[約翰爸爸角度風格指南](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y032) – charlietfl