我有一个角度的应用程序与一个登录页面,应该显示一个加载对话框,而请求正在处理。如果登录在后端成功,我就没有问题了,我会迅速转到内容页面。不幸的是,如果登录失败,加载对话框永远不会隐藏。
下面是我的代码结构:
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();
});
};
}
]);在我的输出中我看到:
login failed
hide
dialog created我想知道我是否误解了承诺是如何工作的,或者在$mdDialog服务内部是否有某种东西在处理某种超时问题。
发布于 2016-06-07 13:57:18
正如您在输出中看到的,只有在登录失败后才创建对话框。在“显示”操作完成后,尝试发出http请求:
app.controller('loginController', [
'$scope',
'$http',
'$mdDialog',
function($scope, $http, $mdDialog) {
var showLoading = function(message, onShown) {
$mdDialog.show({
templateUrl: '../views/loading.html',
controller: function($scope) {
console.log('dialog created');
$scope.message = message;
},
onComplete:onShown
});
};
$scope.credentials = {
username: '',
password: ''
};
$scope.handleLogin = function() {
showLoading('Logging in...', function(){
$http.post('/login', $scope.credentials).then(function success() {
// go to content page
}, function error(response) {
console.log('login failed');
}).finally(function() {
console.log('hide');
$mdDialog.hide();
});
});
};
}
]);发布于 2016-06-07 13:38:05
在“然后”方法中,您可以放置三个函数。
您必须将“$mdDialog.hide()”放在第二个函数中,而不是第三个函数中。第三个函数仅在触发长请求时使用,并且要指示请求前进的百分比。
像这样的事情必须有效:
$http.post('/login', $scope.credentials).then(function success() {
// go to content page
}, function error(response) {
console.log('login failed');
$mdDialog.hide();
});https://stackoverflow.com/questions/37680642
复制相似问题