使用 AngularJS 从选择选项中删除空白选项 [英] Remove Blank option from Select Option with AngularJS
问题描述
我是 AngularJS 的新手.我搜索了很多,但没有解决我的问题.
我第一次在选择框中得到一个空白选项.
这是我的 HTML 代码
<div ng-controller="MyController"><input type="text" ng-model="feed.name" placeholder="Name"/><选择 ng-model="feed.config"><option ng-repeat="configs 中的模板">{{template.name}}</option></选择>
JS
var MyApp=angular.module('MyApp1',[])MyApp.controller('MyController', function($scope) {$scope.feed = {};//配置$scope.configs = [{'name': '配置 1','值':'配置1'},{'name': '配置 2','值':'config2'},{'name': '配置 3','值':'config3'}];//将第一个选项设置为在配置选择中选择$scope.feed.config = $scope.configs[0].value;});
但是好像不行.我怎样才能解决这个问题?这是 JSFiddle 演示
当传递给 ng-options
ng-model 引用的值时,会生成空的 option
>.这样做是为了防止意外的模型选择:AngularJS 可以看到初始模型未定义或不在选项集中,并且不想自行决定模型值.
简而言之:空选项意味着没有选择有效模型(有效我的意思是:从选项集中).您需要选择一个有效的模型值才能去掉这个空选项.
像这样改变你的代码
var MyApp=angular.module('MyApp1',[])MyApp.controller('MyController', function($scope) {$scope.feed = {};//配置$scope.feed.configs = [{'name': '配置 1','值':'配置1'},{'name': '配置 2','值':'config2'},{'name': '配置 3','值':'config3'}];//将第一个选项设置为在配置选择中选择$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="configs 中的模板">{{template.name}}</option></选择>--><select ng-model="feed.config" ng-options="template.value as template.name for template in feed.configs"></选择>
更新(2015 年 12 月 31 日)
如果您不想设置默认值并想删除空白选项,
并且在 JS 中不需要初始化值.
$scope.feed.config = $scope.feed.configs[0].value;
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.
Here is my HTML code
<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;
});
But it doesn't seem to work. How can I get this solved? Here is JSFiddle Demo
For reference : Why does angularjs include an empty option in select?
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.
Change your code like this
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>
UPDATE (Dec 31, 2015)
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>
And in JS no need to initialize value.
$scope.feed.config = $scope.feed.configs[0].value;
这篇关于使用 AngularJS 从选择选项中删除空白选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!