0

我正在为登录表单编写角度服务。我编写了一个工厂(并使用了一个 http 注入器),负责将摘要 HTTP 凭据提交给其余服务。我的身份验证正在工作,但我遇到的问题与错误消息有关,如果提供了不正确的凭据,我想显示该错误消息。

我包含了一个 div 和 ng-show 值 errorAlertTextElem。当资源工厂的 $resolved 状态为 false 时,我想显示 ng-show errorAlertTextElem 元素。现在这有效,但只是片刻。当我输入不正确的凭据并单击登录时,错误消息会显示一秒钟然后消失。我一直在试图弄清楚为什么会这样。我想知道我是否需要使用 $scope.$apply() 但由于摘要已经在进行中,所以这不起作用。我在这里想念什么?

角控制器代码:

//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>
4

2 回答 2

1

我已经解决了这个问题。我正在使用的注射器的位置发生了变化。所以每次注入器运行时,模板都会重新加载。

于 2014-06-30T17:43:47.400 回答
1

仅当发生 http 错误时,角度资源才会拒绝。这意味着当像“404”或“505”这样的错误发生时,解析将是错误的。为了使您的代码正常工作,您应该处理来自服务器的解析响应以显示消息。

test.$promise.resolve(function(response){
     // handle the error come from you server 
     console.log(response);
     // show error msg?

});
于 2014-06-30T00:19:48.107 回答