Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - dynamic ng-options

For the need of a directive i'm writing, i have to construct dynamically the ng-options expression. Here is what i tried.

In my directive:

// ... scope.labelProperty = 'name';
scope.selectOptions = "l." + scope.labelProperty + " for l in list";

In my html template:

<select class="form-control"
    ng-model="selected.available"
    ng-options="{{ selectOptions }}"
    multiple
    size="5"></select>

This results in ng-options taking the correct expression "l.name for l in list" but options dont display.

Please, any idea ?

like image 995
Steph.D Avatar asked May 15 '14 19:05

Steph.D


1 Answers

Change your code to look like this (use javascript to pick your property):

// ... scope.labelProperty = 'name';
scope.selectOptions = "l[labelProperty] for l in list";

<select class="form-control"
    ng-model="selected.available"
    ng-options="{{ selectOptions }}"
    multiple
    size="5"></select>
like image 163
Andrew Church Avatar answered Sep 29 '22 18:09

Andrew Church