gpt4 book ai didi

javascript - Promise、$mdDialog 和操作顺序

转载 作者:行者123 更新时间:2023-11-28 15:08:42 24 4
gpt4 key购买 nike

我有一个带有登录页面的 Angular 应用程序,该页面应该在处理请求时显示加载对话框。如果后端登录成功,就没有问题,我会被转到内容页面。不幸的是,如果登录失败,加载对话框永远不会隐藏。

这是我的代码的结构:

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

我想知道我是否误解了 Promise 的工作原理,或者 $mdDialog 服务内部存在某种正在处理某种超时问题的东西。

最佳答案

正如您在输出中看到的那样,该对话框仅在登录失败后创建。尝试在“show”操作完成后发出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();
});
});
};
}
]);

关于javascript - Promise、$mdDialog 和操作顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37680642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com