选择框中的默认选项 - 角度js

时间:2015-06-29 09:12:18

标签: javascript angularjs

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

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

3 个答案:

答案 0 :(得分:0)

我想你想要“ - ”作为默认值。

变化

<option>--</option>

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

选项-1 :在value=""代码中使用<option>

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

Plunkr

选项-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>

Plunkr