Javascript 暗示使用ng selected=true,如下所示: <select ng-model="myModel"> <option value="a" ng-selected="true">A</option> <option value="b">B</option> </select>
这对我有用Javascript 暗示使用ng selected=true,如下所示: <select ng-model="myModel"> <option value="a" ng-selected="true">A</option> <option value="b">B</option> </select>,javascript,angularjs,html-select,Javascript,Angularjs,Html Select,这对我有用 <select ng-model="somethingHere" ng-init="somethingHere='Cool'"> <option value="Cool">Something Cool</option> <option value="Else">Something Else</option> </select> 这对我有用吗 ng-selected="true" 我需要默认的
<select ng-model="somethingHere" ng-init="somethingHere='Cool'">
<option value="Cool">Something Cool</option>
<option value="Else">Something Else</option>
</select>
这对我有用吗
ng-selected="true"
我需要默认的“请选择”不可选择。我还需要能够有条件地设置默认的选定选项 我通过以下简单的方式实现了这一点: JS代码: //翻转这两个选项,以使用默认的“请选择”文本测试所选的默认值或无默认值 //$scope.defaultOption=0; $scope.defaultOption={key:'3',value:'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:
我希望这能帮助其他有类似需求的人
请选择是通过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>
在你的角度控制器里试试这个 $somethingHere={name:'Something'Cool} 您可以设置一个值,但您使用的是复杂类型,角度搜索键/值将在视图中设置 如果不起作用,请尝试以下方法:
ng options=option.value as option.name for options in options track by option.name问题是我必须为加载的选择框执行此操作。我没有得到它。您需要做什么?我需要手动为50多个选择框设置默认值您的意思是为每个选择框设置默认值?如果是这样的话,我想你的问题还不清楚。不管怎样,我是否正确地假设您必须将项目加载到每个选择框中?如果是这样的话,我不认为初始化它们的默认值有什么大不了的。我的问题是,该选项的索引是从表单上的提交返回的。我需要的是您调用的option.name,它是从一个普通的旧rails表单返回的。复选框和单选按钮工作正常,因为您可以使用ng repeat获得正确的标记。如何在表单提交时返回option.name?此解决方案运行良好,比所选答案更好。ng init不应该是这样的吗?ng init=somethingHere=somethingHere | | options[0]?如何处理选项为空?与选项类似,数据来自外部源。@sud您只需在ng init中检查选项&&options.length>0即可。实际上,大部分操作都应该在控制器中完成,因此它是可测试的。@BenLesh如果我想编写一些文本字符串而不是第一个选项。ng init=somethingHere=somethingHere | |“选择一个”我像这样尝试过。但它不起作用。我的代码出了什么问题我在工作中遇到了一个问题,那就是我使用了“跟踪方式”,删除它修复了问题,以防万一,这有助于解决问题:这个答案没有错,但在我的具体案例中,ng init对我来说失败了。问题是,在运行nginit时,我使用的值还不可用:我通过ajax调用获得了该值,此时nginit已经完成。最后,我在该值上使用了一个监视程序,在该函数中,我使用了与在ng init中相同的方法。“成功了。我也有问题要解决,”毛里特的评论让我享受到了这一点!Angular的文档中说,最好使用控制器$scope来初始化此类值警告:为此目的使用ng init可能会导致对$digest方法的过度调用,这可能会导致控制台输出看起来都是红色和丑陋的,并出现类似10$digest迭代的消息。流产!我曾尝试将其转换为html,但在我尝试过的所有在线转换器中都失败了。为什么不发布html?我是说,像那样发布代码真的有必要吗?在这个问题上,OP并不是在使用模板引擎。这对我来说是有效的。我认为angular需要一些东西来识别我的scope模型中的对象,将其等同于参考列表中的选择。由担保人跟踪。代码成功!如果有一个答案不假设angular被用于构建选项,那就太好了。如果选项已经是标记的一部分怎么办?@pspahn-根据:只有一个**单一**硬编码元素。。。可以嵌套到元素中。选项不能已经是标记的一部分。@pspahn这不会回答OP,但是一个完全不同的问题…这个答案提供零上下文。这是可行的,但不应该被使用,因为ng选项比ng repeat更快、更优化。@Iso不反对您,但是你有这些信息的来源吗?就在文档中:“通过select作为理解表达式的一部分来分配的模型更灵活”,“通过不为每个重复实例创建新范围来减少内存消耗”,“通过在documentFragment中创建选项而不是单独创建选项来提高渲染速度”对我来说很有效。负责绑定我的选择列表选项的异步调用比bro
这对我帮助很大。谢谢伊贝森:-这对我也很有用。简单易懂。最好的方法是使用ng init,因为您正在设置一个ngModel。谢谢它帮助了我;令人惊叹的谢谢
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="option.value as option.name for option in options"
...
$scope.selectedOption = $scope.options[0].value;
<select ng-init="somethingHere = options[0]"
ng-model="somethingHere"
ng-options="option.name for option in options">
</select>
<select ng-model="somethingHere"
ng-init="somethingHere = somethingHere || options[0].value"
ng-options="option.value as option.name for option in options">
</select>
%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
<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>
<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>
<select ng-model="somethingHere.values" ng-options="values for values in [5,4,3,2,1]">
<option value="">Pick a Number</option>
</select>
ng-selected="$first"
<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()
<select ng-model="foo">
<option ng-selected="$first" ng-repeat="(id,value) in myOptions" value="{{id}}">
{{value}}
</option>
</select>
<select ng-model="selectedCity"
ng-options="city as city.name for city in cities track by city.id">
<option value="">-- Select City --</option>
</select>
<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-options="list.key as list.name for list in lists track by list.id" ng-model="selectedItem">
<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
}
});
<select ...
<option value="" ng-selected="selected">Select one option</option>
</select>
ng-init="somethingHere = somethingHere || options[0]"
ng-init="somethingHere = somethingHere || options[0].value"
<select ng-model="myModel">
<option value="a" ng-selected="true">A</option>
<option value="b">B</option>
</select>
<select ng-model="somethingHere" ng-init="somethingHere='Cool'">
<option value="Cool">Something Cool</option>
<option value="Else">Something Else</option>
</select>
ng-selected="true"
$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; }
}
<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>
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>