最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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 = [  { 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...

$scope.model.RemediationCredentials = "Zoo"

Any suggestions?

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 = [  { 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...

$scope.model.RemediationCredentials = "Zoo"

Any suggestions?

Share Improve this question asked Oct 23, 2013 at 18:55 amcdnlamcdnl 8,65813 gold badges65 silver badges104 bronze badges 1
  • Do you need the index attached to the element itself? Would obtaining the index upon selection change suffice? – Jonathan Palumbo Commented Oct 23, 2013 at 19:28
Add a ment  | 

1 Answer 1

Reset to default 4

use the Angular Select Directive:

http://docs.angularjs/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>
发布评论

评论列表(0)

  1. 暂无评论