javascript AngularJS 使用 ng-model 和 ng-repeat 而不是 ng-option 选择默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19550074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS select default value with ng-model and ng-repeat instead ng-option
提问by amcdnl
I have the following code:
我有以下代码:
<select ng-model="model.RemediationCredentials" name="remediationCredential" id="remediationCredential">
<option value="0">---</option>
<option ng:repeat="cred in credentialsList"
data-index="{{$index}}" value="{{cred.Value}}">{{cred.Key}}</option>
</select>
and credentialsList
looks like:
和credentialsList
看起来像:
credentialsList = [ { Key: "Panda", Value: "Zoo: } ]
I think I need to use ng-repeat
vs ng-option
to do some basic things like the no selection and do the data-index
. However, when I try to set the model it doesn't work...
我想我需要使用ng-repeat
vsng-option
来做一些基本的事情,比如没有选择和做data-index
. 但是,当我尝试设置模型时,它不起作用......
$scope.model.RemediationCredentials = "Zoo"
Any suggestions?
有什么建议?
回答by user2643882
use the Angular Select Directive:
使用角度选择指令:
http://docs.angularjs.org/api/ng.directive:select
http://docs.angularjs.org/api/ng.directive:select
Should look something more like this:
应该看起来更像这样:
<select ng-model="model.RemediationCredentials" name="remediationCredential" id="remediationCredential" ng-options="cred.value as cred.key for cred in credentialsList">
<option value="0">---</option>
</select>