Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have data like this :

$scope.list = {
  0:{'type':'text','required':true},
  1:{'type':'text','required':false},
  2:{'type':'text','required':true}
};

i'm using ng-repeat to create form

<div ng-repeat="l in list">
  <input type="{{l.type}}" />
</div>

how I want to get the attributes "required" if the value of "l.required" is true

like this :

<div>
  <input type="text" required />
</div>
<div>
  <input type="text" />
</div>
<div>
  <input type="text" required />
</div>

how I do that

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Try:

<div ng-repeat="(k, l) in list">
  <input type="{{l.type}}" ng-required="l.required" />
</div>

since it is an object that you are iterating through you would need to use the value of the iteration i.e (k, l) in list and use ng-required to set the required flag.

share|improve this answer
    
Why do you need the key? The object is basically an array. –  qwertynl Feb 28 at 14:48
    
yeah, it works. I do not think this would be simple like this :D thx PSL you are very helpful. –  a.fauzi Feb 28 at 14:53
    
yeah, qwertynl true .. I do not need a key. but thanks for the help ;) –  a.fauzi Feb 28 at 14:55
    
@qwertynl object is not an array, but arrays are objects. Here it is an object that you are iterating through so you need to get the value not the key. If you are getting the key then you would need to do list[l].type. Using [] bracket notation on the object does not mean that it is an array, it is a notation to access the properties from an object. –  PSL Feb 28 at 16:05
    
@a.fauzi You are welcome... –  PSL Feb 28 at 16:06
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.