如何自定义AngulrJS中的下拉列表?

时间:2016-09-06 11:38:30

标签: javascript angularjs angularjs-scope

我在AngularJS下拉文本中遇到了一些问题。请在下面查看我的代码

JS

$scope.work_type_list = {
    model: null,
    availableOptions: [
      {id: '1', name: 'Print Ready File', symbol: 'P'},
      {id: '2', name: 'Design Work', symbol: 'D'},
      {id: '3', name: 'Design Only', symbol: 'DO'}
    ]
};

我的HTML

<select class="form-control m-b" name="work_type" ng-model="formData.work_type" ng-options="option.id as option.name for option in work_type_list.availableOptions">
   <option value="">Select Work Type</option>
</select>

我目前的输出是 -

<select ng-options="option.id as option.name for option in work_type_list.availableOptions" ng-model="formData.work_type" name="work_type" class="form-control m-b ng-pristine ng-valid ng-empty ng-touched">
  <option value="" class="" selected="selected">Select Work Type</option>
  <option label="Print Ready File" value="string:1">Print Ready File</option>
  <option label="Design Work" value="string:2">Design Work</option>
  <option label="Design Only" value="string:3">Design Only</option>
</select>

我想添加我的选项文字的PDDO。在这里,我的输出应该是这样的 -

<select ng-options="option.id as option.name for option in work_type_list.availableOptions" ng-model="formData.work_type" name="work_type" class="form-control m-b ng-pristine ng-valid ng-empty ng-touched">
  <option value="" class="" selected="selected">Select Work Type</option>
  <option label="Print Ready File" value="string:1">Print Ready File(P)</option>
  <option label="Design Work" value="string:2">Design Work(D)</option>
  <option label="Design Only" value="string:3">Design Only(DO)</option>
</select>

如何自定义下拉选项文字?

1 个答案:

答案 0 :(得分:5)

您可以在ng-options的各个部分使用表达式:

<select class="form-control m-b" name="work_type" ng-model="formData.work_type"
    ng-options="option.id as (option.name + '(' + option.symbol + ')') for option in work_type_list.availableOptions">
    <option value="">Select Work Type</option>
</select>