簡體   English   中英

搜索功能未按預期工作

[英]Search functionality is not working as expected

我正在使用 angularjs 搜索功能。 正常搜索功能正常工作,沒有任何問題。 我的要求不一樣。

$scope.arrData = [
    "The Community is here",
    "The Community error message" ,
    "This is our country",
    "Error message displayed"
];

我會得到 API 服務的響應。 得到回復后,如果我用隨機關鍵字在搜索框中搜索任何記錄,我將不會得到任何結果。

  • 得到回復后,如果我在文本框中搜索“社區”,我會得到回復[“社區在這里”,“社區錯誤消息”]

  • 但是,實際文本是“社區錯誤消息”,如果我在這里搜索“錯誤消息”,則沒有得到任何結果。 它是空的。 同樣,如果我從上次搜索“消息錯誤”或“消息社區”,我也沒有得到任何結果。

  • 我的需要是,如果我隨機輸入任何單詞,它應該搜索並顯示結果。 例如“錯誤消息”的意思是,它應該顯示[“社區錯誤消息”,“顯示的錯誤消息”]這兩個結果。

$scope.canShow = function (card) {
  if($scope.search){
    return !$scope.search ||
           ($scope.search &&
            card.Tag.toLowerCase().indexOf($scope.search.toLowerCase()) != -1);
  }else{
    return true;
  }
}
<input type="search" class="form-control search-field" ng-model="search">

<div class="object-thumb" ng-repeat="card in value" ng-show="canShow(card)">
   <div class="object-thumb-body">
       <div class="get-object">
          <div class="get-object-wrap">
            <div id="{{ card.objectid }}"></div>
          </div>
       </div>
    </div>
</div>

在這里,card.Tag 是字段,我將在其中獲取響應。

你可以這樣解決你的問題

$scope.arrData= [
    "The Community is here",
    "The Community error message" ,
    "This is our country",
    "Error message displayed"
];

let a = $scope.arrData;
var term = $scope.search; // search term (The Community)
var search = new RegExp(term , 'i'); // prepare a regex object
$scope.value = a.filter(function(item){
  return search.test(item)
});
console.log($scope.value); //here your desire output

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM