如何在 ng-repeat 中使用 select 并在 api 调用数据上保留值 selected
How to use select inside ng-repeat and keep value selected on api call data
我在 ng-repeat 中使用 select。我正在尝试将来自 api 的值映射到 select 作为 selected 值。我不知道我这边出了什么问题,请纠正我。
这里是JSfiddle
HTML代码
<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代码
$scope.trips=[];
$scope.trips=[{id:1,type:'confirmed'},{id:2,type:'cancelled'}];
$scope.tripTypes=['Confirmed','Quotation'];
我猜我犯了这么愚蠢的错误,因为在做了以下更改后我的代码起作用了,我将 ng-model 从 selectedType 更改为 trip.type,所以只有 ng-selected 可以检查条件。
<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>
这里正在工作fiddleCheck now
我在 ng-repeat 中使用 select。我正在尝试将来自 api 的值映射到 select 作为 selected 值。我不知道我这边出了什么问题,请纠正我。 这里是JSfiddle
HTML代码
<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代码
$scope.trips=[];
$scope.trips=[{id:1,type:'confirmed'},{id:2,type:'cancelled'}];
$scope.tripTypes=['Confirmed','Quotation'];
我猜我犯了这么愚蠢的错误,因为在做了以下更改后我的代码起作用了,我将 ng-model 从 selectedType 更改为 trip.type,所以只有 ng-selected 可以检查条件。
<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>
这里正在工作fiddleCheck now