我正在写一个登录表单的角度服务。我已经编写了一个工厂(并使用了一个http注入器),它负责向rest服务提交摘要HTTP凭证。我的身份验证正在工作,但我遇到的问题与错误消息有关,如果提供了不正确的凭据,我希望显示该消息。
我包含了一个带有和ng-show value of errorAlertTextElem的div。当资源工厂的显示状态为false时,我希望显示ng- $resolved errorAlertTextElem元素。现在,这是有效的,但只是一小段时间。当我键入不正确的凭据并单击“登录”时,错误消息会显示一秒钟,然后消失。我一直在试图弄清楚为什么会这样。我想知道是否需要使用$scope.$apply(),但由于digest已经在进行中,所以这不起作用。这里我漏掉了什么?
Angular Controller代码:
//angular.js controller code portion
loriaGameControllers.controller('signinController', ['$scope', 'Users',function($scope,Users){
$scope.signin = function()
{
//Users is a factory that makes a restful resource call.
var test = Users.userInfo().query();//this returns a promise and $resolved
console.log(test);
if(test.$resolved == false){
$scope.errorAlertTextElem = true;//right here is where I attempt to set the ng-show value to true so that the error box will be true. However, the errorAlertTextElem only displays for a second and then goes away.
}
};
}]);html代码登录表单代码(这在我的ng-app中):
<form class="form-signin col-md-6" role="form" name="signinForm" ng-controller="signinController">
<h2 class="form-signin-heading">Sign In</h2>
<!-- The error message I want to show-->
<div class="alert alert-danger" ng-show="errorAlertTextElem">Invalid Username or Password</div>
<input type="email" class="form-control" placeholder="Email address" ng-model="username" name="username"required autofocus>
<input type="password" class="form-control" placeholder="Password" ng-model="password" name="password"required>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<!--ng-click that triggers the callback-->
<div class="btn btn-lg btn-primary btn-block" ng-click="signin()">Sign in</div>
</form>发布于 2014-06-30 08:19:48
只有发生http错误时,angular资源才会被拒绝。这意味着当发生像“404”或“505”这样的错误时,解析将为false。要使您的代码正常工作,您应该处理来自服务器的resolve响应,以显示消息。
test.$promise.resolve(function(response){
// handle the error come from you server
console.log(response);
// show error msg?
});发布于 2014-07-01 01:43:47
我已经解决了这个问题。我用的注射器位置有变化。因此,每次注入器运行时,模板都会重新加载。
https://stackoverflow.com/questions/24481376
复制相似问题