1

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Why do you need the key? The object is basically an array.
yeah, it works. I do not think this would be simple like this :D thx PSL you are very helpful.
yeah, qwertynl true .. I do not need a key. but thanks for the help ;)
@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.
@qwertynl what op is using is not an array it is an object.. you do not have to do that, yes but if you want to acces the properties by writing l in list l will not be the object instead it will be the key... so you would have to do list[l].type so getting the value of the object from ng repeat reduces that.. and you dont have to do list[l]
|

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.