如何在 ng-repeat 中使用 select 并在 api 调用数据上保持选择值 [英] How to use select inside ng-repeat and keep value selected on api call data

查看:13
本文介绍了如何在 ng-repeat 中使用 select 并在 api 调用数据上保持选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ng-repeat 中使用 select.我正在尝试映射来自我的 api 的值以选择作为选定值.我不知道我这边出了什么问题,请纠正我.这是 JSfiddle

HTML 代码

 

{{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></选择>

JS代码

$scope.trips=[];$scope.trips=[{id:1,type:'confirmed'},{id:2,type:'cancelled'}];$scope.tripTypes=['已确认','报价'];

解决方案

我犯了这么愚蠢的错误,我猜是因为在做了以下更改后,我的代码有效了,我将 ng-model 从 selectedType 更改为 trip.type,所以只有它是ng-selected 可以检查条件.

{{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></选择>

这是工作小提琴 立即检查

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'];

解决方案

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

这篇关于如何在 ng-repeat 中使用 select 并在 api 调用数据上保持选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆