Angularjs - 动态 ng-options [英] Angularjs - dynamic ng-options

查看:32
本文介绍了Angularjs - 动态 ng-options的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了需要我正在编写的指令,我必须动态构造 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.

在我的指令中:

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

在我的 html 模板中:

In my html template:

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

这会导致 ng-options 采用正确的表达式l.name for l in list",但选项不显示.

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

请问,有什么想法吗?

推荐答案

将您的代码更改为如下所示(使用 javascript 选择您的属性):

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>

这篇关于Angularjs - 动态 ng-options的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆