角ngMessages更改上课的时候有效,不仅对错误 [英] Angular ngMessages change class when valid and not only on error
问题描述
这是我第一次与ngMessages工作,我的问题是,如果$错误对象有一个对应,如$有效的东西吗?在ngMessages角文档我只穿过$错误对象来了。
It's my first time working with ngMessages and my question is if the $error object has a counterpart, something like $valid? In the angular documentation for ngMessages I only came across the $error object.
<form name="contactForm">
<input ng-class="{'invalid-field': contactForm.nameField.$error, 'valid-field': !contactForm.nameField.$error}" type="text" name="nameField" placeholder="Full name*" required>
<div ng-messages="contactForm.nameField.$error">
<div ng-message="required">Name is required</div>
</div>
</form>
当$错误对象是假的我改变类为有效的场',但这是不正确的我在做什么知道的是。输入字段的格式像它这样,总是有效的,而且是不应该发生。
What I'm doing know is when the $error object is false I change the class to 'valid-field' but this isn't correct. This way the input field is formatted like it's always valid and that isn't supposed to be happening.
推荐答案
根据文档中的NG-模型,其中 $误差
来自于,它也有一个 $有效
状态,以便回答你的问题,是它:)
Based on the documentation the ng-model, where the $error
comes from, it also have a $valid
state so to answer your question, yes it does :)
文档也将告诉你其他验证相关的角模型状态。
Documentation will also show you other validation related angular model states.
https://docs.angularjs.org/api/ng/type/ ngModel.NgModelController
$有效:布尔:真,如果没有错误
$valid: boolean: True if there is no error.
NG-模型
属性应与各个领域的使用虽然当在你这里例如输入不具有 NG-模型
这也可能是问题的根源。
ng-model
attribute should be use with each field though when here in your example the input does not have a ng-model
which could also be source of problem.
这篇关于角ngMessages更改上课的时候有效,不仅对错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!