[英]How can I create a simple <select> in AngularJS with an option pre-selected?
I am trying to create a in AngularJS. 我正在尝试在AngularJS中创建一个。 Here's what I did so far: 这是我到目前为止所做的:
<select ng-model="inverse">
<option value=true>Sort Up</option>
<option value=false selected="selected">Sort Down</option>
</select>
I was trying to set false as the initial value. 我试图将false设置为初始值。 However when I look at the HTML that's created I see this and the select box does not show "Sort Down" as the default: 但是,当我查看创建的HTML时,我看到了这一点,并且选择框未将“排序”显示为默认值:
<select ng-model="inverse" class="ng-pristine ng-valid">
<option value="? undefined:undefined ?"></option>
<option value="true">Sort Up</option>
<option value="false" selected="selected">Sort Down</option>
</select>
Can someone explain what I am doing wrong. 有人可以解释我在做什么错。
Set the value of inverse in the controller using: 使用以下命令在控制器中设置逆值:
$scope.inverse = true;
And remove the selected="selected" attribute from the option. 并从选项中删除selected =“ selected”属性。
This works for me: 这对我有用:
<select ng-model="inverse">
<option value="true">Sort Up</option>
<option value="false">Sort Down</option>
</select>
Controller: 控制器:
$scope.inverse = "false";
However, using ng-options
is suggested. 但是,建议使用ng-options
。
<select ng-init="inverse='false'" ng-model="inverse"> <option value=true>Sort Up</option> <option value=false>Sort Down</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.