使用所选选项的ng-options对象映射ng-model数组值

问题描述 投票:0回答:2

我有以下价值观:

campaign_sms_templates = [11,22];

templateSMSList = [
{"id":11, "name":"test"},
{"id":12, "name":"test 12"},
{"id":22, "name":"test 22"}
];

角度代码如下:

<select multiple="multiple"
   ng-model="campaign_sms_templates"
   ng-options="value as value.name for value in templateSMSList track by value.id">
</select>

但是在渲染时不选择值。当我用以下内容更改campaign_sms_templates参数值时:

campaign_sms_templates = [ {"id":11}, {"id":22} ];

它作为对象匹配,在渲染时显示选定的值。

任何人都可以帮助,我如何使用这些值渲染选定的选项:

campaign_sms_templates = [11,22];

如果有任何需要更多说明,请告诉我。

angularjs components multi-select bootstrap-multiselect
2个回答
0
投票

花了很多时间在这之后,现在我终于解决了这个问题

<select multiple="multiple" ng-change="save(campaign_sms_templates)" ng-model="campaign_sms_templates">
    <option value=[[field.id]] ng-repeat="field in templateSMSList" ng-selected="campaign_sms_templates.indexOf(field.id)!==-1">[[field.name]]</option>
</select>

这似乎不是一个好习惯,但因为我只想在我的数据库文档中保存id所以我这样做了。

欢迎任何反馈。


0
投票

使用value.id as value.name for value

<select multiple="multiple"
   ng-model="campaign_sms_templates"
   ng-options="value.id as value.name for value in templateSMSList">
</select>

同样省略track by expression,因为它不需要,并将打破指令。

有关更多信息,请参阅

© www.soinside.com 2019 - 2024. All rights reserved.