在选择选项中包含ng显示

时间:2014-09-06 02:27:38

标签: angularjs angularjs-scope angularjs-ng-repeat

我使用ng-options填充选择。在值中,它给出了一个addon_id(来自json数据),名称是addon_name。这可以使用以下代码:

              <div ng-show="group.group_optiontype == 'single'">
                <select ng-model="foo1" ng-options="singleAddon.addon_name for singleAddon in group.addons track by singleAddon.addon_id" name="addon-val-single" class="form-control-wrapper col-xs-12 col-sm-12 col-lg-12">
                   <option value="" ng-hide="foo1">choose something</option>
                </select>
              </div>

但我也有一个名为addon_priceoption的变量(可以是'免费'或'付费'),如果付费我想要显示addon_price ...现在我如何将此登录信息包含在我的代码中?我试过了:

              <div ng-show="group.group_optiontype == 'single'">
                <select ng-model="foo1" ng-options="singleAddon.addon_name for singleAddon in group.addons track by singleAddon.addon_id" name="addon-val-single" class="form-control-wrapper col-xs-12 col-sm-12 col-lg-12">
                   <option value="" ng-hide="foo1">choose something<span ng-show="singleAddon.addon_priceoption == 'paid'">{{singleAddon.addon_price}}</span></option>
                </select>
              </div>

这不起作用....最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

您可以在范围内编写一个函数,如下所示

$scope.formattedLabel = function(singleAddon){
  if(singleAddon.addon_proceoption =='paid'){
   return singleAddon.addon_name + singleAddon.addon_price;
  }else{
   return singleAddon.addon_name;
  }
};

将ng-options更改为

ng-options="singleAddon.addon_name as formattedLabel(singleAddon) for singleAddon in group.addons track by singleAddon.addon_id"

您也可以使用过滤器来完成此操作。