AngularJS选择使用ng-model和ng-repeat而非ng-option的默认值

时间:2013-10-23 18:55:25

标签: javascript angularjs

我有以下代码:

<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>

credentialsList看起来像:

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

我想我需要使用ng-repeat vs ng-option来做一些基本的事情,比如没有选择,然后执行data-index。但是,当我尝试设置模型时,它不起作用......

$scope.model.RemediationCredentials = "Zoo"

有什么建议吗?

1 个答案:

答案 0 :(得分:4)

使用Angular Select指令:

http://docs.angularjs.org/api/ng.directive:select

应该看起来像这样:

<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>