我已经搜索过Google,但找不到任何东西。
我有这个代码。
<select ng-model="somethingHere"
ng-options="option.value as option.name for option in options"
></select>
有了这样的数据
options = [{
name: 'Something Cool',
value: 'something-cool-value'
}, {
name: 'Something Else',
value: 'something-else-value'
}];
输出是这样的。
<select ng-model="somethingHere"
ng-options="option.value as option.name for option in options"
class="ng-pristine ng-valid">
<option value="?" selected="selected"></option>
<option value="0">Something Cool</option>
<option value="1">Something Else</option>
</select>
如何将数据中的第一个选项设置为默认值,这样您将得到这样的结果。
<select ng-model="somethingHere" ....>
<option value="0" selected="selected">Something Cool</option>
<option value="1">Something Else</option>
</select>
您可以像这样简单地使用ng-init
<select ng-init="somethingHere = options[0]"
ng-model="somethingHere"
ng-options="option.name for option in options">
</select>
如果要确保$scope.somethingHere
视图初始化时不会覆盖您somethingHere = somethingHere || options[0].value
的值,则可以在ng-init中合并值(),如下所示:
<select ng-model="somethingHere"
ng-init="somethingHere = somethingHere || options[0].value"
ng-options="option.value as option.name for option in options">
</select>
尝试这个:
的HTML
<select
ng-model="selectedOption"
ng-options="option.name for option in options">
</select>
Java脚本
function Ctrl($scope) {
$scope.options = [
{
name: 'Something Cool',
value: 'something-cool-value'
},
{
name: 'Something Else',
value: 'something-else-value'
}
];
$scope.selectedOption = $scope.options[0];
}
如果您确实要设置将绑定到模型的值,则将ng-options
属性更改为
ng-options="option.value as option.name for option in options"
和Javascript
...
$scope.selectedOption = $scope.options[0].value;
另一个Plunker这里考虑以上。
只有一个答案通过Srivathsa哈里什Venkataramana提到track by
这确实是一个解决方案!
这是一个示例以及Plunker(下面的链接)如何track by
在select中 使用ng-options
:
<select ng-model="selectedCity"
ng-options="city as city.name for city in cities track by city.id">
<option value="">-- Select City --</option>
</select>
如果selectedCity
是在角范围限定,并且它具有id
属性具有相同值的任何id
的任何city
的上cities
列表中,这将是在负载选定的自动。
这是为此的Plunker:http ://plnkr.co/edit/1EVs7R20pCffewrG0EmI?p=preview
有关更多详细信息,请参见源文档:https :
//code.angularjs.org/1.3.15/docs/api/ng/directive/select
我认为,在包含“ track by”之后,您可以在ng-options中使用它来获取所需的内容,如下所示
<select ng-model="somethingHere" ng-options="option.name for option in options track by option.value" ></select>
这样做的方式更好,因为当您要用对象列表替换字符串列表时,只需将其更改为
<select ng-model="somethingHere" ng-options="object.name for option in options track by object.id" ></select>
当然,这里的某物是具有属性名称和id的对象。请注意,“ as”不会以这种方式表示ng-options,因为它只会设置值,并且在使用track by时将无法更改它
可接受的答案使用ng-init
,但文件说尽可能避免ng-init。
ngInit的唯一适当用法是为ngRepeat的特殊属性添加别名,如下面的演示所示。除了这种情况,您应该使用控制器而不是ngInit来初始化作用域上的值。
您也可以使用ng-repeat
代替来ng-options
进行选择。有了ng-repeat
,你可以使用ng-selected
与ng-repeat
特殊属性。即$ index,$ odd,$ even无需任何编码即可完成这项工作。
$first
是ng-repeat的特殊属性之一。
<select ng-model="foo">
<option ng-selected="$first" ng-repeat="(id,value) in myOptions" value="{{id}}">
{{value}}
</option>
</select>
----------------------编辑----------------
尽管可以,但我还是希望@ mik-t当您知道要选择的值时,请回答https://stackoverflow.com/a/29564802/454252,使用track-by
和ng-options
不使用ng-init
或ng-repeat
。
仅当您必须选择第一个项目而不知道选择哪个值时,才应使用此答案。例如,我正在使用它进行自动完成,这需要一直选择第一项。
我的解决方案是使用html硬编码默认选项。像这样:
在HAML中:
%select{'ng-model' => 'province', 'ng-options' => "province as province for province in summary.provinces", 'chosen' => "chosen-select", 'data-placeholder' => "BC & ON"}
%option{:value => "", :selected => "selected"}
BC & ON
在HTML中:
<select ng-model="province" ng-options="province as province for province in summary.provinces" chosen="chosen-select" data-placeholder="BC & ON">
<option value="" selected="selected">BC & ON</option>
</select>
我希望我的默认选项从我的api返回所有值,这就是为什么我有一个空白值。还请原谅我。我知道这不是OP的问题的直接答案,但是人们可以在Google上找到它。希望这对其他人有帮助。
使用下面的代码填充模型中的选定选项。
<select id="roomForListing" ng-model="selectedRoom.roomName" >
<option ng-repeat="room in roomList" title="{{room.roomName}}" ng-selected="{{room.roomName == selectedRoom.roomName}}" value="{{room.roomName}}">{{room.roomName}}</option>
</select>
根据您拥有的选项数量,您可以将值放入数组中,然后像这样自动填充选项
<select ng-model="somethingHere.values" ng-options="values for values in [5,4,3,2,1]">
<option value="">Pick a Number</option>
</select>
就我而言,我只需要插入一个初始值来告诉用户选择一个选项,所以,我喜欢下面的代码:
<select ...
<option value="" ng-selected="selected">Select one option</option>
</select>
当我尝试使用值为!=的空字符串(空)的选项时,该选项将替换为angular,但是,当放置这样的选项(具有null值)时,select将会出现此选项。
对不起,我的英语不好,我希望能对此有所帮助。
select
与ngOptions
和一起使用并设置默认值:
有关更多用法示例,请参见ngOptions文档ngOptions
。
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: {id: '2', name: 'Option B'} //This sets the default value of the select in the ui
};
}]);
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
<body 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.name for option in data.availableOptions track by option.id"
ng-model="data.selectedOption"></select>
</form>
<hr>
<tt>option = {{data.selectedOption}}</tt><br/>
</div>
有关HTML的官方文档SELECT
带有角度数据绑定的元素的。
select
通过ngModel
解析/格式化绑定到非字符串值:
(function(angular) {
'use strict';
angular.module('nonStringSelect', [])
.run(function($rootScope) {
$rootScope.model = { id: 2 };
})
.directive('convertToNumber', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
ngModel.$parsers.push(function(val) {
return parseInt(val, 10);
});
ngModel.$formatters.push(function(val) {
return '' + val;
});
}
};
});
})(window.angular);
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.1/angular.min.js"></script>
<body ng-app="nonStringSelect">
<select ng-model="model.id" convert-to-number>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
{{ model }}
</body>
其他例子:
angular.module('defaultValueSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.availableOptions = [
{ name: 'Apple', value: 'apple' },
{ name: 'Banana', value: 'banana' },
{ name: 'Kiwi', value: 'kiwi' }
];
$scope.data = {selectedOption : $scope.availableOptions[1].value};
}]);
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
<body ng-app="defaultValueSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<select ng-model="data.selectedOption" required ng-options="option.value as option.name for option in availableOptions"></select>
</form>
</div>
</body>
这对我有用。
<select ng-model="somethingHere" ng-init="somethingHere='Cool'">
<option value="Cool">Something Cool</option>
<option value="Else">Something Else</option>
</select>
就我而言,由于默认格式在每种情况下都不同。我在select标记中添加了一个自定义属性。
<select setSeletected="{{data.value}}">
<option value="value1"> value1....
<option value="value2"> value2....
......
在指令中,我创建了一个脚本来检查该值,并在将其填充角度时将选项设置为选中。
.directive('setSelected', function(){
restrict: 'A',
link: (scope, element, attrs){
function setSel=(){
//test if the value is defined if not try again if so run the command
if (typeof attrs.setSelected=='undefined'){
window.setTimeout( function(){setSel()},300)
}else{
element.find('[value="'+attrs.setSelected+'"]').prop('selected',true);
}
}
}
setSel()
})
只是从咖啡翻译中翻译过来的,至少它的原理是正确的,如果不是漏洞的话。
这不是最简单的方法,但是要在值变化时完成它
回应Ben Lesh的回答,应该有这条线
ng-init="somethingHere = somethingHere || options[0]"
代替
ng-init="somethingHere = somethingHere || options[0].value"
那是,
<select ng-model="somethingHere"
ng-init="somethingHere = somethingHere || options[0]"
ng-options="option.name for option in options track by option.value">
</select>
只需使用ng-selected="true"
如下:
<select ng-model="myModel">
<option value="a" ng-selected="true">A</option>
<option value="b">B</option>
</select>
我将在控制器中设置模型。然后,选择将默认为该值。例如:html:
<select ng-options="..." ng-model="selectedItem">
角度控制器(使用资源):
myResource.items(function(items){
$scope.items=items;
if(items.length>0){
$scope.selectedItem= items[0];
//if you want the first. Could be from config whatever
}
});
这为我工作
ng-selected="true"
如果要使用ng-options
渲染,则下拉菜单将option
默认选择具有与ng-modal相同的值。考虑示例:
<select ng-options="list.key as list.name for list in lists track by list.id" ng-model="selectedItem">
因此,默认选择list.key
与和具有相同值的 selectedItem
选项。
我需要默认的“请选择”才能取消选择。我还需要能够有条件地设置默认的选定选项。
我通过以下简单的方法实现了此目的:JS代码://翻转这2个以测试所选的默认值或不使用默认的“ Please Select”文本//没有默认值//$scope.defaultOption = 0; $ scope.defaultOption = {键:'3',值:'Option 3'};
$scope.options = [
{ key: '1', value: 'Option 1' },
{ key: '2', value: 'Option 2' },
{ key: '3', value: 'Option 3' },
{ key: '4', value: 'Option 4' }
];
getOptions();
function getOptions(){
if ($scope.defaultOption != 0)
{ $scope.options.selectedOption = $scope.defaultOption; }
}
HTML:
<select name="OptionSelect" id="OptionSelect" ng-model="options.selectedOption" ng-options="item.value for item in options track by item.key">
<option value="" disabled selected style="display: none;"> -- Please Select -- </option>
</select>
<h1>You selected: {{options.selectedOption.key}}</h1>
我希望这可以帮助其他有类似要求的人。
“请选择”是通过Joffrey Outtier在此处的回答完成的。
如果您有一些事情,而不仅仅是将日期部分初始化,则可以ng-init()
在控制器中通过声明来使用它,并在HTML顶部使用它。该函数将像控制器的构造函数一样工作,您可以在其中初始化变量。
angular.module('myApp', [])
.controller('myController', ['$scope', ($scope) => {
$scope.allOptions = [
{ name: 'Apple', value: 'apple' },
{ name: 'Banana', value: 'banana' }
];
$scope.myInit = () => {
$scope.userSelected = 'apple'
// Other initiations can goes here..
}
}]);
<body ng-app="myApp">
<div ng-controller="myController" ng-init="init()">
<select ng-model="userSelected" ng-options="option.value as option.name for option in allOptions"></select>
</div>
</body>
<!--
Using following solution you can set initial
default value at controller as well as after change option selected value shown as default.
-->
<script type="text/javascript">
function myCtrl($scope)
{
//...
$scope.myModel=Initial Default Value; //set default value as required
//..
}
</script>
<select ng-model="myModel"
ng-init="myModel= myModel"
ng-options="option.value as option.name for option in options">
</select>
在您的角度控制器中尝试一下...
$ somethingHere = {名称:“很酷的东西”};
您可以设置一个值,但是您使用的是复杂类型,角度将搜索要在视图中设置的键/值。
并且,如果不起作用,请尝试以下操作:ng-options =“ option.value as option.name for option in option by by option.name”
我认为最简单的方法是
ng-selected="$first"
文章标签:angularjs , html-select , javascript
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!