我在Angular中有一个表单,其中包含字段行,其中每一行都是一个ng-repeat
块:
<form name="myForm><table>
<tr class="item-row" ng-repeat="item in items">
<td>
<input type="text" ng-model="item.width" ng-change="processItem(item)">
</td>
<td>
<input type="text" ng-model="item.relatedWidth" ng-change="processItem(item)">
</td>
</tr>
</table></form>
在我的$scope.processItem(item)
方法中,我希望仅当for字段为原始字段时才item.relatedWidth
匹配的值,即用户未对其进行编辑。如果用户编辑该字段,则应将其解耦。item.width
item.relatedWidth
用伪代码,我想这样做:
$scope.processItem = function(item){
if(the field for item.relatedWidth is pristine){
item.relatedWidth = item.width;
}
}
我可以看到该字段具有类ng-pristine
,ng-dirty
但我不知道如何以Angular方式获取此值。
鉴于字段的动态呈现方式,我如何读取该字段的$ pristine / $ dirty状态ng-repeat
?
您需要向ng-form
每行添加指令,name
向输入添加属性,并将relatedWidth输入的原始状态传递给processItem
函数:
<form name="myForm">
<table>
<tr class="item-row" ng-repeat="item in items" ng-form="itemForm">
<td>
<input type="text" name="width" ng-model="item.width"
ng-change="processItem(item, itemForm.related_width.$pristine)">
</td>
<td>
<input type="text" name="related_width" ng-model="item.relatedWidth"
ng-change="processItem(item)">
</td>
</tr>
</table>
</form>
控制器:
$scope.processItem = function(item, relatedPristine){
if(relatedPristine === true){
item.relatedWidth = item.width;
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句