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.

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);

But I am getting this:

TypeError: Cannot read property '$pristine' of undefined
4
  • 2
    The error is telling you that $scope.myform.price is undefined. Why is it undefined? Commented Nov 16, 2017 at 15:48
  • @Mayday My assumption is that it's not taking into account the price variable as an input name but i'm not sure why this is.Even though it is being populated as price_1 Commented Nov 16, 2017 at 15:53
  • How did you define $scope.myform.price? In your code, you only defined a string variable called price Commented Nov 16, 2017 at 16:02
  • I haven't defined it. Should I be doing so? What i'm essentially trying to accomplish is if i use manually console.log($scope.myform.price_1.$pristine) it returns a value but if I set a variable with the field name and the line number and use it within console.log($scope.myform.price.$pristine) it breaks it. Commented Nov 16, 2017 at 16:05

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.