1

I'm generating a lot of HTML with ng-repeat. I rely on the $index variable in order to index data in my controller.

I do alot of stuff like

 ng-submit="validateExistingGuest($index)" 

In this case, an undetermined number of HTML forms is generated, hence the index.

Problem is, i sometimes need to use this variable inside a different kind of expression. That would look something like that:

ng-if= "user{{$index}}.valid"

Of course, that doesn't work. I tried ways of constructing that expression, with no success.

How would one go about doing this?

1
  • is user{{$index}} the name of your form? Commented Jan 27, 2018 at 19:24

3 Answers 3

1

You need something like

 ng-if="user[$index].valid
Sign up to request clarification or add additional context in comments.

Comments

0
<div ng-repeat="value in user track by $index"> ... 
  //now you can use the {{$index}}
  <div ng-if="user[$index].valid">Show only if valid!</div>
</div>

1 Comment

@sheff2k1If the question is answered, you should close the question by accepting an answer.
0

Why don't you use the object instead the $index?, try

<div ng-repeat="car in cars" > 
    <p {{car}}></p>
    <button ng-click="buy(car)" > buy </button>
</div>

1 Comment

I'm using the index, because i'm generating an unknown number of HTML forms, with a corresponding unknown number of user objects :-)

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.