将字符串用作输入名称时,无法读取未定义的属性"$ pristine" [英] Cannot read property '$pristine' of undefined when string used as input name
问题描述
在我的表单中,我有一个ng-repeat,在该ng-repeat中,我有一堆字段.有时可能有3个字段,有时可能只有1个字段.
In my form I have an ng-repeat and within that ng-repeat I have a bunch of fields. Sometimes there could be 3 fields, sometimes there might just be one.
我每个人都有一个角度来检查$ pristine的特定字段.字段的名称几乎保持不变,但是字段计数的数字发生了变化,所以我想我会像这样检查它:
I have an angular for each to check $pristine for a specific field. The name of the field pretty much stays the same but the field count number changes so I thought I would check it like this:
var price = "price_" + product.line_no;
console.log($scope.myform.price.$pristine); = console.log($scope.myform."price_1".$pristine);
但是我得到了:
TypeError: Cannot read property '$pristine' of undefined
推荐答案
确保已在输入字段中设置了唯一名称属性.这是一个提琴手,可以帮助 https://jsfiddle.net/paka2/n6weLg55/ >
Make sure that you have set unique name attribute in your input field. Here is a fiddler that can help https://jsfiddle.net/paka2/n6weLg55/
<div ng-repeat="product in products">
<input name="price_{{$index}}" type="text" ng-model="productList['price_' + $index]" required/>
</div>
这篇关于将字符串用作输入名称时,无法读取未定义的属性"$ pristine"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!