什么是之间的差异!$质朴VS在angularjs $脏质朴、差异、angularjs、VS

2023-09-13 05:19:33 作者:蝴蝶爲花醉、花卻自枯萎

最近,我读了一些教程的angularJS表单验证,像这样的:

 < p NG秀=$ userForm.email无效和放大器;&安培;!userForm.email $质朴。>< / P> 

但我觉得!$质朴 $脏都是平等的,所以我可以用下面的一个呢?

 < p NG秀=$ userForm.email无效和放大器;&安培; userForm.email $脏>< / P> 

解决方案

我觉得有这两个属性之间的细微差别,这取决于你的使用情况。

$使用setDirty(); 设置形式脏状态。这种方法可以称为加入'NG-脏'类,并设置形式脏状态(NG-脏类)。这种方法也将传播到父窗体。

$ setPristine(); 设置形式到它的原始状态。这种方法可以称为以删除'NG-脏'类和表单设置为它的原始状态(NG-原始类)。这种方法也将传播到包含在这种形式的所有控件。设置窗体回原始状态往往是有用的,当我们想'重用'保存或复位后一种形式。

所以,你可以同时使用$原始和$以不同的方式变脏。虽然,您可以检查该文件由macrog在下面的文章中提到。

AngularJs双向绑定详解

Recently I read some tutorial about angularJS form validation, like this:

<p ng-show="userForm.email.$invalid && !userForm.email.$pristine"></p>

but I think !$pristine and $dirty are equal, so can I use the below one instead?

<p ng-show="userForm.email.$invalid && userForm.email.$dirty"></p>

解决方案

I think there is a slight difference between these two attributes, which depends on your use case.

$setDirty(); Sets the form to a dirty state. This method can be called to add the 'ng-dirty' class and set the form to a dirty state (ng-dirty class). This method will also propagate to parent forms.

$setPristine(); Sets the form to its pristine state. This method can be called to remove the 'ng-dirty' class and set the form to its pristine state (ng-pristine class). This method will also propagate to all the controls contained in this form. Setting a form back to a pristine state is often useful when we want to 'reuse' a form after saving or resetting it.

So, you can use both $pristine and $dirty in a different way. Though, you can check the document as mentioned by 'macrog' in the below post.

 
精彩推荐
图片推荐