AngularJS验证不起作用

AngularJS validations not working?

本文关键字:不起作用 验证 AngularJS      更新时间:2023-09-26

我是AngularJS的新手,我刚刚开始调整它附带的验证,但由于某种原因我无法让它们工作:

<div class="row">
    <div class="col-xs-6">
        <h2>Login</h2>
        <p class="text-danger">{{message}}</p>
        <form name="form" novalidate>
            <label>Email: </label>
            <input type="email" ng-model="credentials.email" placeholder="Email" class="form-control" required />
            <label>Password: </label>
            <input type="password" ng-model="credentials.password" placeholder="Password" class="form-control" />
            <br />
            <button class="btn btn-primary" ng-click="authenticate(credentials)">Login</button>
            <a href="#/register">Register</a>
        </form>
    </div>
</div>

我命名了表格并放了novalidate。然后我尝试根据需要输入电子邮件,但将属性放在那里。但是表单仍然提交,AngularJS不会阻止它提交或显示任何警告。我错过了什么?

为输入提供名称字段

 <form name="form" novalidate>
            <label>Email: </label>
            <input type="email" name="email" ng-model="credentials.email" placeholder="Email" class="form-control" required />
            <label>Password: </label>
            <input type="password" ng-model="credentials.password" placeholder="Password" class="form-control" />
            <br />
            <button class="btn btn-primary" ng-disabled="form.$invalid" ng-click="authenticate(credentials)">Login</button>
            <a href="#/register">Register</a>
        </form>

这是 plunker :-)http://plnkr.co/edit/pAA2wBp8zgIZV6QLwO7S?p=preview

如果要将其设为可选,请从电子邮件输入中删除required属性。

<input type="email" ng-model="credentials.email" placeholder="Email" class="form-control"/>

如果要删除电子邮件验证,请更改字段类型。

HTML5 始终验证电子邮件,如果您type="email"

<input type="text" ng-model="credentials.email" placeholder="Email" class="form-control"/>