首页 > 解决方案 > 在 angularjs ui-select 指令中更新后,如何默认设置搜索字段中的更新值?

问题描述

我在 angularjs ui-select 指令中遇到问题。它工作正常,它在下拉列表中显示了完整的数据。当我从搜索字段中选择一个值并想要更新它时,它会更新,但默认情况下它不能显示在搜索字段中。我必须再次手动搜索才能看到更新的值。让我粘贴代码...

这是angularjs代码

$scope.consignee = [];
$http.get("get-consignee", {
 }).then(function(response){

 $scope.consignee = response.data;
 //$scope.consignee.selected = $scope.consignee[0];
 });

这是用户界面选择代码

 <ui-select ng-model="consignee.selected" theme="select2">

 <ui-select-match placeholder="Select Consignee">

    <% $select.selected.CONSIGNEE_NAME %>

 </ui-select-match>

 <ui-select-choices ng-repeat="e in consignee | filter: $select.search">

   <div><% e.CONSIGNEE_NAME %></div>

 </ui-select-choices>

 </ui-select>

假设我在下拉列表中有 5 个收货人姓名!

 1. hamad
 2. test2
 3. yasin Gul
 4. hamid
 5. munir

所以问题是,当我使用它时,它会$scope.consignee.selected = $scope.consignee[0];0索引处给出hamad更新后的名称,即使我进行了更新test2,或者yasin Gulhamad在搜索字段中默认设置为我。我知道我有0, 1, 2, 3,4索引,但我希望它是动态的而不是手动的。我只想设置我更新的那个名字。如果我更新test2,以便它应该test2在更新后默认设置在搜索字段中给我,对于yasin Gul等。任何帮助将不胜感激谢谢

在此处输入图像描述

标签: javascriptangularjsangular6laravel-5.7angular-ui-select

解决方案


cons_id是您选择的数据库 ID。

$scope.consignee = [];
$http.get("get-consignee", {
 }).then(function(response){

 $scope.consignee = response.data;
 var index = $scope.consignee.findIndex(x => x.CONSIGNEE_ID==cons_id); // use this
 $scope.consignee.selected = $scope.consignee[index]; 
});

推荐阅读