使用AngularJS从选择选项中删除空白选项 [英] Remove Blank option from Select Option with AngularJS
问题描述
我是AngularJS的新手.我进行了很多搜索,但是并不能解决我的问题.
I am new to AngularJS. I searched a lot, but it does not solve my problem.
我第一次在选择框中得到一个空白选项.
I am getting a blank option for the first time in select box.
这是我的HTML代码
<div ng-app="MyApp1">
<div ng-controller="MyController">
<input type="text" ng-model="feed.name" placeholder="Name" />
<select ng-model="feed.config">
<option ng-repeat="template in configs">{{template.name}}</option>
</select>
</div>
</div>
JS
var MyApp=angular.module('MyApp1',[])
MyApp.controller('MyController', function($scope) {
$scope.feed = {};
//Configuration
$scope.configs = [
{'name': 'Config 1',
'value': 'config1'},
{'name': 'Config 2',
'value': 'config2'},
{'name': 'Config 3',
'value': 'config3'}
];
//Setting first option as selected in configuration select
$scope.feed.config = $scope.configs[0].value;
});
但是它似乎不起作用. 我该如何解决?这是 JSFiddle演示
But it doesn't seem to work. How can I get this solved? Here is JSFiddle Demo
推荐答案
供参考:为什么angularjs在select <中包含空选项?
当
ng-model
引用的值在传递给ng-options
的一组选项中不存在时,将生成空的option
.这样做是为了防止意外选择模型:AngularJS可以看到初始模型未定义或不在选项集中,并且不想自己决定模型的值.
The empty
option
is generated when a value referenced byng-model
doesn't exist in a set of options passed tong-options
. This happens to prevent accidental model selection: AngularJS can see that the initial model is either undefined or not in the set of options and don't want to decide model value on its own.
简而言之:空选项意味着没有选择有效的模型(有效的意思是:从选项集中).您需要选择一个有效的模型值来摆脱此空选项.
In short: the empty option means that no valid model is selected (by valid I mean: from the set of options). You need to select a valid model value to get rid of this empty option.
像这样更改代码
var MyApp=angular.module('MyApp1',[])
MyApp.controller('MyController', function($scope) {
$scope.feed = {};
//Configuration
$scope.feed.configs = [
{'name': 'Config 1',
'value': 'config1'},
{'name': 'Config 2',
'value': 'config2'},
{'name': 'Config 3',
'value': 'config3'}
];
//Setting first option as selected in configuration select
$scope.feed.config = $scope.feed.configs[0].value;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="MyApp1">
<div ng-controller="MyController">
<input type="text" ng-model="feed.name" placeholder="Name" />
<!-- <select ng-model="feed.config">
<option ng-repeat="template in configs">{{template.name}}</option>
</select> -->
<select ng-model="feed.config" ng-options="template.value as template.name for template in feed.configs">
</select>
</div>
</div>
更新(2015年12月31日)
如果您不想设置默认值并想删除空白选项,
If You don't want to set a default value and want to remove blank option,
<select ng-model="feed.config" ng-options="template.value as template.name for template in feed.configs">
<option value="" selected="selected">Choose</option>
</select>
并且在JS中无需初始化值.
And in JS no need to initialize value.
$scope.feed.config = $scope.feed.configs[0].value;
这篇关于使用AngularJS从选择选项中删除空白选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!