【问题标题】:AngularJS filter searchAngularJS 过滤器搜索
【发布时间】:2018-12-18 17:25:59
【问题描述】:

我已经尝试了从角度到无序列表之一的过滤逻辑。 但是,搜索过滤器在输入名称的 3 个字符后起作用,有时会给出错误的搜索结果。

 <input type='text' ng-model='searchString' placeholder="Search a name..." />


<ul class="nav">
     <li class="active" ng-repeat="item in artists.People | filter :searchString">
           <a href ng-click='setSelectedArtist(item)'>{{item.name}}</a>
     </li>
 </ul>

JSON:

{

    "People":[
       {
          "name":"Andrew Amernante",
          "rating":3,
          "img":"http://www.fillmurray.com/200/200",
          "Description":"Gluten­free cray cardigan vegan. Lumbersexual pork belly blog, fanny pack put a bird on it selvage",
          "Likes":[
             "Dogs",
             "Long walks on the beach",
             "Chopin",
             "Tacos"
          ],
          "Dislikes":[
             "Birds",
             "Red things",
             "Danish food",
             "Dead Batteries"
          ]
       },
       {
          "name":"Frank Wang",
          "rating":5,
          "img":"http://www.fillmurray.com/200/201",
          "Description":"Before errors, mails were only pressures. This is not to discredit the idea that a magic is the prose of anelizabeth. This could be, or perhaps some posit the outmost coil to be less than dedal. Some assert that those treatments are nothing more than carp.",
          "Likes":[
             "Frank",
             "Manchester United",
             "Football",
             "Programming"
          ],
          "Dislikes":[
             "Dogs",
             "Long walks on the beach",
             "Chopin",
             "Tacos"
          ]
       },
       {
          "name":"Sissi Chen",
          "rating":5,
          "img":"http://www.fillmurray.com/200/202",
          "Description":"Aaah! Natural light! Get it off me! Get it off me! Oh, loneliness and cheeseburgers are a dangerous mix. D'oh. Here's to alcohol, the cause of all life's problems.",
          "Likes":[
             "Cats",
             "the beach",
             "Chopin",
             "Blue things"
          ],
          "Dislikes":[
             "Birds"
          ]
       },
       {
          "name":"Diego Garcia",
          "rating":2,
          "img":"http://www.fillmurray.com/200/204",
          "Description":"Facts are meaningless. You could use facts to prove anything that's even remotely true! I prefer a vehicle that doesn't hurt Mother Earth. It's a go­cart, powered by my ownsense of self­satisfaction. You don't win friends with salad.",
          "Likes":[
             "Talking Spanish",
             "Spanish food",
             "Spanish things",
             "Football"
          ],
          "Dislikes":[
             "Not talking spanish",
             "Chopin"
          ]
       },
       {
          "name":"Fuad Rashid",
          "rating":4,
          "img":"http://www.fillmurray.com/200/206",
          "Description":"Gluten­free cray cardigan vegan. Lumbersexual pork belly blog, fanny pack put a bird on it selvage",
          "Likes":[
             "Dogs",
             "Long walks on the beach",
             "Chopin",
             "Tacos"
          ],
          "Dislikes":[
             "Birds",
             "Red things",
             "Danish food",
             "Dead Batteries"
          ]
       }
    ]
 }

这是plnkr 代码。

例如:开始输入“si”,您将得到两个结果,其中第一个(frank wang)不正确。

而且,这是我所指的引用 plnkr

【问题讨论】:

    标签: javascript angularjs filter angularjs-filter


    【解决方案1】:

    您需要指定要过滤的对象属性,在本例中为 namefilter

    <input type='text' ng-model='searchString' placeholder="Search a name..." />
    
    <ul class="nav">
      <li class="active" ng-repeat="item in artists.People | filter: { name: searchString }">
        <a href ng-click='setSelectedArtist(item)'>{{item.name}}</a>
      </li>
    </ul>
    

    您还需要将searchString 的初始值设置为空字符串,以便在未输入任何文本时匹配所有人。

    $scope.searchString = '';
    

    这是一个 Plunker 演示功能。

    希望对您有所帮助!

    【讨论】:

    • 数组还没有从上面的代码sn-p加载
    • 您没有为searchString 指定初始值。如果你设置$scope.searchString = ''; 应该加载它。让我更新答案和 Plunker。这确实解决了输入字符时名称显示不正确的问题。
    【解决方案2】:

    您可以创建自己的自定义过滤器来指定需要搜索的属性:

    $scope.searchTextByName = function(artist){
        if($scope.searchText !== undefined){
            if(artist.name.toLowerCase().indexOf($scope.searchText.toLowerCase()) > -1 ){
               return artist;
            }
        }
    }
    

    否则,它将与您的 searchText 键匹配单个人对象的所有 JSON 值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多