简体   繁体   English

Angular JS Ng-Show / Ng-隐藏

[英]Angular JS Ng-Show / Ng-Hide

I am writing an angular service for a login form. 我正在为登录表单编写角度服务。 I have written a factory (and used an http injector) that takes care of submitting the digest HTTP credentials to the rest service. 我已经编写了一家工厂(并使用了http注入器),负责将摘要HTTP凭据提交给其余服务。 My Authentication is working but the problem I am having has to do with an error message that I would like to display if incorrect credentials are supplied. 我的身份验证正常,但是我遇到的问题与错误消息有关,如果提供了错误的凭据,我将显示该错误消息。

I included a div with and ng-show value of errorAlertTextElem. 我包括了一个带有errorAlertTextElem的ng-show值的div。 When the $resolved status for resource factory is false I want to show the ng-show errorAlertTextElem element. 当资源工厂的$ resolved状态为false时,我想显示ng-show errorAlertTextElem元素。 Right now this works but only for a moment. 目前,此方法有效,但仅需片刻。 When I type in incorrect credentials and click sign in the error messages displays for a second and then goes away. 当我输入不正确的凭据并单击登录时,错误消息将显示一秒钟,然后消失。 I have been trying to figure out why this is. 我一直试图找出原因。 I wondered if I needed to use $scope.$apply() but since digest is already in progress this does not work. 我想知道是否需要使用$ scope。$ apply(),但是由于摘要已经在进行中,因此无法正常工作。 What am I missing here? 我在这里想念什么?

Angular Controller code: 角度控制器代码:

//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 Code Signin Form Code (This is inside my ng-app): 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>

The angular resource will only reject when an http error happened. 只有在发生http错误时,角度资源才会拒绝。 That means when an error like '404' or '505' happened the resolve will be false. 这意味着,当发生类似“ 404”或“ 505”的错误时,解决方案将为假。 To make your code work you should handle the resolve response that comes from server to show the message. 要使代码正常工作,您应该处理来自服务器的resolve响应以显示消息。

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

});

I have resolved the issue. 我已经解决了这个问题。 There was a location change in an injector I was using. 我使用的进样器位置发生了变化。 So every time the injector ran, the template was reloaded. 因此,每次进样器运行时,都会重新加载模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM