我是新手。我对$dirty
和$invalid
感到困惑,它们听起来几乎一样。$dirty
$invalid
中使用的email
和ng-model
有什么区别?以下是scenario
。这是一个示例表W3Schools。
<form ng-app="myApp" ng-controller="validateCtrl" name="myForm" novalidate>
<p>
Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color: red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<span ng-show="myForm.user.$error.required">Username is required.</span>
</span>
</p>
<p>
Email:<br>
<input type="email" name="emaill" ng-model="email" required>
<span style="color: red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>
<p>
<input type="submit" ng-click="Count()"
ng-disabled="myForm.$invalid" title="Submit" value="Submit">
</p>
</form>
编辑1:
我想知道是否将ng-model名称从
email
更改为email8
,它不再起作用。<input type="email" name="emaill" ng-model="email8" required>
验证是否通过
myForm
HTML元素名称(未使用ng
属性定义)进行。它是如何工作的?ng-show="myForm.email.$dirty && myForm.email.$invalid"
最佳答案
$dirty
:如果用户已经与表单进行了交互,则为TRUE。 $invalid
:如果至少一个包含表单和控件的表单无效,则为TRUE。 Source
Also on Angular Docs
在Question ...中更新后,正在对表单元素名称进行验证。跟踪表单中的所有ng模型,这就是它的工作方式。
同样,更改ng-model名称不会对验证产生影响。我尝试了您的链接,它对我有用。那必须工作。
关于angularjs - $ dirty与$ invalid : What is the difference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33821928/