Dennis Dennis - 18 days ago 5
AngularJS Question

Use variable inside ng-repeat

I'm trying to find a way to insert my variable(s) into ng-repeat,
see examples below for explaination.

controller.js

$scope.firstParams = $stateParams.firstId;


template.html

<span style="margin-left:3px;" ng-repeat="list in user.userlists.{{firstParams}}"></span>


ng-repeat example is just for displaying purpose

basically what i want to do is having my ng-repeat fetch the params via $scope.firstParams and have it like so: ng-repeat="list in user.userlists.gameboy" if the params would be "gameboy".

I've searched far and hopefully wide but i cant seem to find a solution that fits, i tried a bunch of filter:Options with no success.

Anyone got a solution for this? Much thanks in advantage!

Answer

In javascript you use [] notation for variable property names

Try

ng-repeat="list in user.userlists[firstParams]"

Demo Link - http://jsfiddle.net/tk11wz7j/