0

My AngularJS controller (called 'search') has the variables:

this.redInfo = [...];
this.blueInfo = [...];
this.greenInfo = [...];
this.tableSelector = ''; //this can either be 'red', 'blue', or 'green'

I'm trying to do an ng-repeat where the info that gets repeated is dependent on the tableSelector. Is this possible? This is what I have now:

<div ng-repeat="row in {{'search.' + search.tableSelector + 'Info'}} track by $index">

2 Answers 2

1

What you're currently attempting isn't possible. You can make a method that returns the appropriate repeat array:

this.getColorArray = function() {
   if (this.search.tableSelector == "red") return this.redInfo
   ...
}

Then repeat over that:

<div ng-repeat="row in search.getColorArray() track by $index">

Depending on the array site and other watchers on the page, this could impact performance. If you know where tableSelector changes - you can always set your repeat array at that point and avoid the extra overhead in the $digest cycles.

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

Comments

1

Use a bracket notation property accessor:

<div ng-repeat="row in search[search.tableSelector + 'Info'] track by $index">

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.