【问题标题】:Map ng-model array values with the object of ng-options for selected options将 ng-model 数组值与所选选项的 ng-options 对象映射
【发布时间】:2019-08-15 06:57:18
【问题描述】:

我有以下价值观:

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];

如果有什么需要进一步说明的,请告诉我。

【问题讨论】:

  • 好吧 select 就是这样工作的,你介意在绑定到视图之前将你的数组映射到对象数组吗?
  • 感谢@supercool 在花了很多时间后才解决了这个问题 :( 请检查并分享您的反馈。

标签: angularjs components multi-select bootstrap-multiselect


【解决方案1】:

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

<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 保存在我的数据库文档中,所以我这样做了。

欢迎任何反馈。

【讨论】:

    【解决方案2】:

    使用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,因为它不需要并且会破坏指令。

    有关详细信息,请参阅

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多