Array = {
0 : { NAME: 'A' , PET :'DOG' , AGE : 'NINE' },
1 : { NAME: 'B' , PET :'CAT' , AGE : 'TEN' },
2 : { NAME: 'C' , PET :'TURTLE' , AGE : '' }
}
Html :
<option ng-repeat='data in array' value="data.NAME"> {{data.PET}} </option>
So, if second(index) option is selected and click on a save button. I want to show an error saying age is empty.
I have an access to only Name because of value with val() and PET with text() in :selected
How to show an error if age is empty
2
Answers
Few observations :
Your
Array
variable is not holding an array, It is holding an object.Your
ng-repeat
syntax should be changed to handle the object of objects.On submit, You can check for
AGE
field value from theArray
object against this selected value (value.NAME
).Live Demo :
You can just search for the right object by
NAME
using array.find()
method, then check over theAGE
value of this one, if it exists:Demo:
Note:
And make sure to arrange your
ng-repeat
to handle objects instead of array, because you have an object here.