javascript - Angular JS Ng-显示/Ng-隐藏

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

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

我包含了一个带有 errorAlertTextElem 值的 div 和 ng-show 值。当资源工厂的 $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>

最佳答案

我已经解决了这个问题。我使用的注入(inject)器位置发生了变化。因此,每次注入(inject)器运行时,都会重新加载模板。

关于javascript - Angular JS Ng-显示/Ng-隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24481376/

相关文章:

javascript - 总结输入值

javascript - 鼠标悬停在 d3 矩形上不起作用

javascript - 删除并破坏 angularjs 中特定位置的 json 数据

angularjs - $state.go() 和 $state.transitionTo() 之间的区别

javascript - 如何在 AngularJS ng-click 触发器之前设置超时

angularjs - ionic 在右键单击和中键单击时触发不需要的 ng-click

angularjs - 添加后加载后,ng-click不会触发

javascript - 如何在three.js中先加载JSON模型并在init之前分配一个变量?

javascript - 查询字符串总是在服务器端返回 NULL

javascript - 如何获取 $http.get() 的 url 值以在函数内部使用?