0
ngMessage用于在表单验证失败时显示错误消息。但是现在需要这个div在http 500发生错误时显示。当http错误发生时,我可以得到相应的错误信息,但是当时不知道如何显示ngMessage div。Angular如何在登录页面上显示http错误消息
HTML:
<div class="error_msg" ng-if="form_login.$submitted" ng-messages="form_login.$error" flex="initial">
<div class="alertmsg" ng-message="required" flex">{{errormsg | i18next}}</div>
</div>
控制器:
loginModule.controller("loginController", ['$http', '$window', '$rootScope', '$scope', '$location', 'authenticationService', '$i18next', function($http, $window, $rootScope, $scope, $location, authenticationService, $i18next){
$scope.newclass = false;
$scope.errormsg = 'login.alertmsg';
$scope.login = function(){
if($scope.form_login.$invalid){
return false;
}else{
$scope.newclass = true;
$scope.dataloading = true;
authenticationService.login($scope.username, $scope.password,
function(response){
if(response.data.status_code == 200){
$window.location.href = 'view/home.html';
}else if(response.data.status_code == 500){
$scope.errormsg = 'error_code' + '.' +response.data.payload.error_code;
console.log($scope.errormsg);
$scope.dataloading = false;
$scope.newclass = false;
}
},
function(response){
$scope.dataloading = false;
$scope.newclass = false;
});
}
};
}]);
更新: 如果我只使用纳克 - 如果,既可以显示形式验证消息和HTTP错误消息,但没有ngMessage,形式验证消息将不会是动态的
您需要做的是将您的身份验证服务用作异步验证程序。异步验证已添加到AngularJS 1.3。 – georgeawg