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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:56:54  来源:igfitidea点击:

AngularJS select default value with ng-model and ng-repeat instead ng-option

javascriptangularjs

提问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 credentialsListlooks like:

credentialsList看起来像:

credentialsList = [  { Key: "Panda", Value: "Zoo: } ]

I think I need to use ng-repeatvs ng-optionto 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-repeatvsng-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>