繁体   English   中英

选择框中的默认选项-angular js

[英]default option in select box - angular js

 <select name="country" id="countries" ng-options="country.name for country in countries track by country.id" style="font-size:13px;" ng-model="sel_country"
      ng-change="set(sel_country)" >

      <option>--</option>

</select>

// countries object
$scope.countries = [{name: 'UNITED KINGDOM', id: 1 },{name: 'AUSTRIA', id: 2}];

如何使用上述代码在选择框中显示默认选项?

我想您想将“-”作为默认值。

更改

<option>--</option>

<option value="">--</option>
<select name="country" 
    id="countries" 
    ng-options="country.name for country in countries track by country.id" 
    style="font-size:13px;" 
    ng-model="sel_country"
    ng-change="set(sel_country)" >
    <option value="" disabled selected>--</option>
</select>

Option-1 :在您的<option>标记内使用value=""

 <select name="country" id="countries" ng-options="sel_country as country.name for country in countries track by country.id" style="font-size:13px;" ng-model="sel_country[0]"
  ng-change="set(sel_country)" >

        <option value="">-select-</option>

  </select>

普伦克

选项2 :将ng-initng-model

 <select ng-init="sel_country = countries[0]" name="country" id="countries" ng-options="sel_country as country.name for country in countries track by country.id" style="font-size:13px;" ng-model="sel_country"
  ng-change="set(sel_country)" >

        <option>-select-</option>

  </select>

普伦克

暂无
暂无

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

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