【问题标题】:angular - ng-show wont remove the ng-hide class when form field becomes invalidangular - 当表单字段无效时,ng-show 不会删除 ng-hide 类
【发布时间】:2014-12-17 18:09:41
【问题描述】:

ng-show 在表单上有它的 ng-hide 类,当字段变得无效时,它应该删除该类并显示错误消息,但由于某种原因,它不起作用

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form id="email-form" name="email-form" ng-controller="loginFormCtrl as ctrl" >
            <div>
                <input ng-model="user.name" ng-minlength="5" ng-maxlength="10" class="w-input username" id="name" type="text" placeholder="Enter your name" name="name" data-name="Name"  required>
                <span ng-show="email-form['name'].dirty && email-form['name'].$error.maxlength">too long!</span>
                <span ng-show="email-form.name.dirty && email-form.name.$error.minlength">too short!</span>
            </div>
            <div>
                <input ng-model="user.email" class="w-input" id="email" type="email" placeholder="Enter your email address" name="email" data-name="Email" required>
                <span ng-show="email-form['email'].dirty &&  email-form.email.$error.required">required</span>
                <span ng-show="email-form.email.dirty &&  email-form.email.$error.required">not an email</span>
            </div>
            <div class="w-row">
                <div class="w-col w-col-6 w-col-stack"><a class="button widebutton" ng-click="submit()" href="">Log In</a>
                </div>
                <div class="w-col w-col-6 w-col-stack"><a class="button widebutton"  href="" ng-click="go('/createaccount')">Sign Up</a>
                </div>
            </div>
        </form>

【问题讨论】:

  • $dirty 不是dirty
  • 就是这样!结合下面的答案:) 非常感谢。

标签: html angularjs validation ng-show


【解决方案1】:

将您的表单名称 name="email-form" 更改为 name="emailForm"。在documentation 之后,您应该使用类似:

<span ng-show="emailForm.email.$dirty && emailForm.email.$error.required">required</span>
<span ng-show="emailForm.email.$dirty && emailForm.email.$error.email">not an email</span>

与其他两个相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多