2

Angular 似乎没有在下面的代码中引发 minLength 或 maxLength 错误……但是,所需的错误(以及电子邮件错误)正在起作用。我知道 ng-minlength 和 ng-maxlength 正在工作,因为输入框正在更改其 CSS。里面的文本<span>不适用于最小或最大错误。

请参阅下面的密码输入:

<section class="row" data-ng-controller="AuthenticationController">
<h3 class="col-md-12 text-center">Sign Up</h3>
<div class="col-xs-offset-2 col-xs-8 col-md-offset-5 col-md-2">
    <form name="userForm" data-ng-submit="userForm.$valid &amp;&amp; signup()" class="signin form-validate" novalidate autocomplete="off">
        <fieldset>
            <div class="form-group">
                <span ng-show="userForm.email.$dirty &amp;&amp; userForm.email.$error.required" class="text-danger">This field is required</span>
                <span ng-show="userForm.email.$dirty &amp;&amp; userForm.email.$error.email" class="text-danger">This field must be a valid email address</span>
            </div>
            <div class="form-group">
                <label for="username" class="control-label">Username</label>
                <input type="text" id="username" name="username" class="form-control" data-ng-model="credentials.username" placeholder="Username">
            </div>
            <div class="form-group">
                <label for="password" class="control-label">Password</label>
                <input type="password" id="password" name="password" class="form-control" required data-ng-model="credentials.password" ng-minlength="8" ng-maxlength="24" placeholder="Password">
                <span ng-show="userForm.password.$dirty &amp;&amp; userForm.password.$error.required" class="text-danger">This field is required</span>
                <span ng-show="userForm.password.$dirty &amp;&amp; userForm.password.$error.minLength" class="text-danger">Please use a password of at least 8 characters</span>
                <span ng-show="userForm.password.$dirty &amp;&amp; userForm.password.$error.maxLength" class="text-danger">The charactar limit for passwords is 24</span>
            </div>
            <div class="text-center form-group mt">
                <button type="submit" class="btn btn-large btn-primary">Sign up</button>&nbsp; or&nbsp;
                <a href="/#!/page/signin" class="show-signup">Sign in</a>
            </div>
            <div data-ng-show="error" class="text-center text-danger">
                <strong data-ng-bind="error"></strong>
            </div>
        </fieldset>
    </form>
</div>

对这里出了什么问题有任何想法吗?

4

2 回答 2

1

这只是一个语法错误:userForm.password.$error.minLength应该是userForm.password.$error.minlength(大写)。

于 2017-02-11T08:52:55.733 回答
0

您应该更改&amp;&amp;&&内部ng-show表达式,您必须在控制台中遇到错误。

更新后的ng-show版本如下所示。

ng-show="userForm.password.$dirty && userForm.password.$error.required"

另一种更方便的方法是使用ng-messages指令,根据表单及其字段有效性显示和隐藏验证消息

于 2016-06-24T15:54:21.217 回答