angularjs - angular ng-repeat expressions as variables -


i'm trying this:

<ul>     <li ng-repeat="{{myrepeatexpression}}">{{row.name}}</li> </ul> 

but because ng-repeat logic in compile state of directive treats {{myrepeatexpression}} normal string instead of variable. doesn't work, obviously.

is there workaround that?

you can use , expression ng-repeat , not interpolated value. in order create dynamic repeatable list can try either:

  1. using function returns list dynamically in ng-repeat - this potentially more expensive since angular needs call function first determine if collection has changed when doing $digest cycle
  2. $watch particular variable on scope trigger change of list - potentially more efficient if dynamic list depends on more 1 variable can more verbose , can lead potential bugs forgetting add new $watch when new variable required

demo plunker

js:

app.controller('mainctrl', function($scope) {   var values1 = [{name:'first'}, {name:'second'}];   var values2 = [{name:'third'}, {name:'fourth'}, {name:'fifth'}];    //1. function way   $scope.getvalues = function(id) {     if(id === 1) {       return values1;     }     if(id === 2) {       return values2;     }   }    //2. watch way   $scope.values = undefined;   $scope.$watch('id', function(newval) {     $scope.values = $scope.getvalues(newval);   }); }); 

html:

<!-- here pass required value directly function --> <!-- not mandatory can use other scope variables and/or private variables --> <ul>   <li ng-repeat="v in getvalues(id)">{{v.name}}</li> </ul> <!-- nothing special here, plain old ng-repeat --> <ul>   <li ng-repeat="v in values">{{v.name}}</li> </ul> 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -