select 非空选项后默认选项消失
Default Option disappears after select non-empty option
我有我的问题的简单示例 JSFiddle。
一开始我有 empty/default 选项,但是当我从下拉菜单中选择其他选项时,这个选项就消失了。
如何在select之后离开这个选项?
<label for="mySelect">Make a choice:</label>
<select name="mySelect" id="mySelect"
ng-options="option.name for option in data.availableOptions"
ng-model="data.selectedOption"></select>
Angular-控制器:
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
selectedOption: {id: '2', name: 'Option B'} //This sets the default value of the select in the ui
};
我尝试使用 ng-init
将默认选项设置为 null
,但这不起作用
请在 select 元素内添加空选项元素并检查。
<select name="mySelect" id="mySelect"
ng-options="option.name for option in data.availableOptions"
ng-model="data.selectedOption">
<option></option>
</select>
使用这种方法:
HTML
<div ng-app="defaultValueSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<label for="mySelect">Make a choice:</label>
<select name="mySelect" id="mySelect"
ng-options="option.id as option.name for option in data.availableOptions"
ng-model="data.selectedOption"></select>
</form>
<hr>
<tt>option = {{data.selectedOption}}</tt><br/>
</div>
AngulerJS
angular.module('defaultValueSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
selectedOption: '2' //This sets the default value of the select in the ui
};
}]);
我有我的问题的简单示例 JSFiddle。
一开始我有 empty/default 选项,但是当我从下拉菜单中选择其他选项时,这个选项就消失了。
如何在select之后离开这个选项?
<label for="mySelect">Make a choice:</label>
<select name="mySelect" id="mySelect"
ng-options="option.name for option in data.availableOptions"
ng-model="data.selectedOption"></select>
Angular-控制器:
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
selectedOption: {id: '2', name: 'Option B'} //This sets the default value of the select in the ui
};
我尝试使用 ng-init
将默认选项设置为 null
,但这不起作用
请在 select 元素内添加空选项元素并检查。
<select name="mySelect" id="mySelect"
ng-options="option.name for option in data.availableOptions"
ng-model="data.selectedOption">
<option></option>
</select>
使用这种方法:
HTML
<div ng-app="defaultValueSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<label for="mySelect">Make a choice:</label>
<select name="mySelect" id="mySelect"
ng-options="option.id as option.name for option in data.availableOptions"
ng-model="data.selectedOption"></select>
</form>
<hr>
<tt>option = {{data.selectedOption}}</tt><br/>
</div>
AngulerJS
angular.module('defaultValueSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
selectedOption: '2' //This sets the default value of the select in the ui
};
}]);