简体   繁体   English

角获取data-ng-options的标签,但将值传递给ng-model

[英]Angular get label of data-ng-options but pass value to ng-model

Id like to pass the value of the data-ng-options to the ng-model but get the label of the value to display on the span as below , How would i go about it ? 我想将data-ng-options的值传递给ng-model但获得值的标签显示在跨度上,如下所示,我将如何处理?

View 视图

<tr ng-repeat="choice in vm.choices">
    <td>
        <span>@{{choice.department }}</span>
      <div class="form-group">
         <div class="input-group">
          <select data-ng-options=
                'd.value as d.label for d in vm.departments' ng-model="choice.department">
                    </select>
         </div>
      </div>
    </td>
</tr>

controller 控制者

vm.departments = [
            {value:0, label:'DENTAL'},
            {value:1, label:'PYCHOLOGY'}

          ];

id like the label ie 'DENTAL' to display on the span but pass the value ie 0 to the ng-model id像标签“ DENTAL”一样显示在span上,但是将值0传递给ng-model

Storing the value only in the ng-model makes no sense. 仅在ng-model存储值没有意义。 Storing the object is the standard method when dealing with an array of objects in a dropdown. 在下拉列表中处理对象数组时,存储对象是标准方法。

Consider the following: 考虑以下:

<span>@{{choice.department.value }}</span>
<span>{{choice.department.label}}</span>
<div class="form-group">
  <div class="input-group">
    <select data-ng-options='d.label for d in vm.departments' 
            ng-model="choice.department">
    </select>
  </div>
</div>

http://plnkr.co/edit/MGIvAAhiSsgkxSXpwTWZ?p=preview http://plnkr.co/edit/MGIvAAhiSsgkxSXpwTWZ?p=preview

You have access to all the properties of the object to use for whatever you need. 您可以访问该对象的所有属性,以用于所需的任何内容。

The difference here is in the way that the ng-options is structured. 此处的区别在于ng-options的结构方式。 By not supplying an as clause, the entire object is assigned to the ng-model parameter, instead of a specific property from the object. 通过不提供as子句,整个对象被分配给ng-model参数,而不是对象的特定属性。

Transform the array into object looking like this: 将数组转换为对象,如下所示:

{
  0: 'DENTAL',
  1: 'PSYCHOLOGY'
}

and store it in separate scope variable (let's say dict ), then in template use: 并将其存储在单独的范围变量中(比如说dict ),然后在模板中使用:

{{dict[choice.department]}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM