How to use select inside ng-repeat and keep value selected on api call data

一笑奈何 提交于 2019-12-11 17:27:54

问题


I am using select inside ng-repeat. I am trying to map value coming from my api to select as selected value. I don't know what's wrong going from my side, please correct me. Here is JSfiddle

HTML code

 <div ng-repeat="trip in trips">
        {{trip.type}}
                    <select ng-change="typeChanged(selectedType,trip.id)" id="" name="" ng-model="selectedType">
<option    ng-selected="trip.type === t" ng-repeat="t in tripTypes">{{t}}</option>
</select>
      </div>

JS code

$scope.trips=[];
  $scope.trips=[{id:1,type:'confirmed'},{id:2,type:'cancelled'}];

      $scope.tripTypes=['Confirmed','Quotation'];

回答1:


I was making so silly mistake i guess because after doing below changes my code worked, i changed ng-model to trip.type from selectedType, so only it is possible for ng-selected to check condition.

<div ng-repeat="trip in trips">
{{trip.type}}
<select ng-change="typeChanged(selectedType,trip.id)" id="" name="" ng-model="trip.type">
<option    ng-selected="trip.type === t" ng-repeat="t in tripTypes">{{t}}</option>
</select>
</div>

Here is working fiddle Check now



来源:https://stackoverflow.com/questions/50305993/how-to-use-select-inside-ng-repeat-and-keep-value-selected-on-api-call-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!