AngularJS ng-selected 指令

定義和用法

ng-selected 指令設置 <select> 列表中 <option> 元素的 selected 屬性。

如果 ng-selected 屬性內的表達式返回 true,則將選擇該選項。

ng-selected 指令是能夠在 true 和 false 之間轉換值所必需的。在 HTML 中,不能將 selected 屬性設置為 false(selected 屬性的存在會使元素被選中,無論其值如何)。

實例

將 selected 屬性設置為 true:

請單擊此處選擇 BMW 作為您最喜歡的汽車:

<input type="checkbox" ng-model="mySel">
<p>我最喜歡的汽車:</p>
<select>
<option>Volvo</option>
<option ng-selected="mySel">BMW</option>
<option>Ford</option>
</select>

親自試一試

語法

<option ng-selected="expression"></option>

<option> 元素支持。

參數

參數 描述
expression 如果返回 true,則將設置元素的 selected 屬性的表達式。