[英]How to avoid empty automatic option in an angularjs select?
<form ng-show="editingUser" class="form-inline editingUser" role="form" name="gebruikerdetailform" novalidate>
<input type="text" class="form-control" ng-model="userDetail.name" placeholder="Naam" ng-required="true"/>
<input type="text" class="form-control" ng-model="userDetail.surname" placeholder="Surname"/>
<select class="form-control" ng-model="userDetail.role">
<option ng-selected="userDetail.role === 'user' || userDetail.role === ''" >user</option>
<option ng-selected="userDetail.role === 'admin'">admin</option>
</select>
</form>
in this code, automatically the empty option <option value="? string: ?"></option>
is created by angularjs. 在此代码中,由angularjs自动创建空选项<option value="? string: ?"></option>
。 i know it happens when a value referenced by ng-model doesn't exist in a the ng-options . 我知道它会在ng-options中不存在ng-model引用的值时发生。 but this is not the case, and i don't understand how to avoid this behavior, and where does it come from. 但这不是事实,我不知道如何避免这种行为以及它来自何处。 any ideas? 有任何想法吗?
If the problem concerns the initialization of the ng-model , you can use the directive ng-init
, as follows: 如果问题与ng-model的初始化有关,则可以使用指令ng-init
,如下所示:
<form ng-show="editingUser" class="form-inline editingUser" role="form" name="gebruikerdetailform" novalidate>
<input type="text" class="form-control" ng-model="userDetail.name" placeholder="Naam" ng-required="true"/>
<input type="text" class="form-control" ng-model="userDetail.surname" placeholder="Surname"/>
<select class="form-control" ng-model="userDetail.role" ng-init="user">
<option ng-selected="userDetail.role === 'user' || userDetail.role === ''" >user</option>
<option ng-selected="userDetail.role === 'admin'">admin</option>
</select>
</form>
Look at the usage of the ng-init
directive in the select definition. 查看select定义中ng-init
指令的用法。
For more info: https://docs.angularjs.org/api/ng/directive/ngInit 有关更多信息: https : //docs.angularjs.org/api/ng/directive/ngInit
Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.