Hackerman
Hackerman

Reputation: 1378

Concatenation in HTML using AngularJS

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">

Upvotes: 0

Views: 27

Answers (2)

georgeawg
georgeawg

Reputation: 48948

Use a bracket notation property accessor:

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

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

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.

Upvotes: 1

Related Questions