我有一个角度的应用程序与一个登录页面应该显示加载对话框,而请求正在处理。如果登录在后端成功,我就没有问题了,然后我马上离开内容页面。不幸的是,如果登录失败,加载对话框不会被隐藏。
这里是我的代码的结构:我想知道如果我也许误解的承诺是如何工作的或可能存在于内部的东西
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