2015-09-20 39 views
0

我是AngularJS的初学者。我尝试了解有关“搜索”选项卡的示例。修改搜索栏的输出(AngularJS)

代码在这里:http://www.tutorialspoint.com/angularjs/angularjs_search_tab.htm

我的问题:由于默认情况下,我想隐藏在控制器的$ scope.items我用NG-重复项目的结果。

当我在酒吧搜索,只有我想寻求的结果可以显示在屏幕上。

我该怎么做?

+0

尝试NG-躲来躲去的项目,直到搜索不是不确定的。 –

回答

0

更改代码过滤这

app.filter('searchFor', function(){ 

// All filters must return a function. The first parameter 
// is the data that is to be filtered, and the second is an 
// argument that may be passed with a colon (searchFor:searchString) 

return function(arr, searchString){ 

    var result = []; 

    if(searchString){ 

     searchString = searchString.toLowerCase(); 

     // Using the forEach helper method to loop through the array 
     angular.forEach(arr, function(item){ 

      if(item.title.toLowerCase().indexOf(searchString) !== -1){ 
       result.push(item); 
      } 

     }); 
    } 

    return result; 
}; 

}); 
0

使用NG-模型输入,然后通过管道在ngRepeat表达了built in angular filter的项目,有你想要的行为。

<input type="text" ng-model="query"> 
    <ul> 
     <li ng-repeat="item in items | filter:query">{{item}}</li> 
    </ul> 

入住这demo