简体   繁体   English

使用Angular.js验证表单输入的原始状态

[英]The original state of using Angular.js to validate form input

the code below is part of signin.html. 以下代码是signin.html的一部分。 when i visit that page, nick and password is surely empty, so the myForm.nick.$error.required is true . 当我访问该页面时,昵称和密码肯定为空,因此myForm.nick.$error.requiredtrue the error message is displayed. 显示错误信息。 What i want is, when i visited the page, there's no error message on the page. 我想要的是,当我访问页面时,页面上没有错误消息。 What should i do? 我该怎么办? Thanks 谢谢

<form ng-submit="signin()" name="myForm">
  <input type="text" name="nick" ng-model="data.nick" required>
  <span class="error" ng-show="myForm.nick.$error.required">Required</span><br>
  <input type="password" name="password" ng-model="data.password" required>
  <span class="error" ng-show="myForm.password.$error.required">Required</span><br>
</form>

You should add a $dirty condition to prevent the required field message being displayed at the beginning of page. 您应该添加$ dirty条件,以防止在页面开头显示必填字段消息。

myForm.nick.$error.required && myForm.nick.$dirty
myForm.password.$error.required && myForm.password.$dirty

$dirty===true means that user has already interacted with the form. $ dirty === true表示用户已经与表单进行了交互。

Here is a jsfiddle demo 这是一个jsfiddle演示

Hope this helpful. 希望对您有所帮助。

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

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