select value does not shows default value when using ng-repeat

时间:2015-10-18 14:16:57

标签: html angularjs drop-down-menu angularjs-ng-repeat

Why select tag with ng-options works correctly by showing the default selected value but same is not working if I use with ng-repeat.

here is the code,

NullPointerException

in app.js,

  <!-- this works-->

<select class="form-control" ng-model="selectValue" ng-options="option.id as option.name for option in availableOptions"> 
</select>

<!-- this also works-->

    <select ng-model="selectValue">
       <option ng-repeat="options in availableOptions" value="{{options.id}}" ng-selected="selectValue === options.id">{{options.name}}</option>
    </select> 

<!-- this doesnt works-->

<select ng-model="selectValue">
   <option ng-repeat="options in availableOptions" ng-value="options.id" ng-selected="selectValue === options.id">{{options.name}}</option>
</select> 

Though the HTML shows the selected attribute, still its not rendered properly in the view

Though the HTML shows the selected attribute, still its not rendered properly in the view

Here is the plunker link for the same

1 个答案:

答案 0 :(得分:0)

虽然在网页上下拉的正确方法应该是使用ng-options,但您可以更改下面的标记,以使其与ng-repeat版本一起使用。

<强>标记

<select ng-model="selectValue">
   <option ng-repeat="options in availableOptions" value="{{options.id}}">
     {{options.name}}
   </option>
</select>

Working Plunkr