Angular JS Ng-Show / Ng-Hide

时间:2014-06-30 00:01:41

标签: javascript angularjs angularjs-ng-click ng-show

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

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

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>

2 个答案:

答案 0 :(得分:1)

角度资源仅在发生http错误时拒绝。这意味着当发生'404'或'505'之类的错误时,解析将是错误的。要使代码工作,您应该处理来自服务器的解析响应以显示消息。

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

});

答案 1 :(得分:1)

我已经解决了这个问题。我正在使用的喷油器发生了位置变化。因此,每次进样器运行时,模板都会重新加载。