我有一個角度的應用程序與一個登錄頁面應該顯示加載對話框,而請求正在處理。如果登錄在後端成功,我就沒有問題了,然後我馬上離開內容頁面。不幸的是,如果登錄失敗,加載對話框不會被隱藏。
這裏是我的代碼的結構:我想知道如果我也許誤解的承諾是如何工作的或可能存在於內部的東西
login failed
hide
dialog created
:
app.controller('loginController', [
'$scope',
'$http',
'$mdDialog',
function($scope, $http, $mdDialog) {
var showLoading = function(message) {
$mdDialog.show({
templateUrl: '../views/loading.html',
controller: function($scope) {
console.log('dialog created');
$scope.message = message;
}
});
};
$scope.credentials = {
username: '',
password: ''
};
$scope.handleLogin = function() {
showLoading('Logging in...');
$http.post('/login', $scope.credentials).then(function success() {
// go to content page
}, function error(response) {
console.log('login failed');
}).then(function() {
console.log('hide');
$mdDialog.hide();
});
};
}
]);
在我的輸出我看到正在處理某種超時的$mdDialog
服務。
你應該用角加載條http://chieffancypants.github.io/angular-loading-bar/所有'$ http'請求到後端。 – nextt1
你是否期待那第二個()作爲終止函數?如果是這樣......將其更改爲finally(function(){});請參閱https://docs.angularjs.org/api/ng/service/$q以瞭解Promise API部分。 – Zach
@Zach我也試過,但在對話框創建並顯示之前仍然被調用 – pulse0ne