简体   繁体   English

使用AngularJ过滤搜索不起作用

[英]Filter search with AngularJs not working

I am trying to adapt the following search filter seen here but with little sucesss. 我正在尝试调整以下此处看到的搜索过滤器但收效甚微。 What am I getting wrong? 我怎么了?

This is my code so far: 到目前为止,这是我的代码:

controllers.js controllers.js

angular.module('starter.controllers', ['starter.services'])

.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
})

.controller('SearchCtrl', ["$scope", "ServicesData", function($scope, ServicesData, filterFilter)  {

  // Get list items

  $scope.categoryList = ServicesData.getAllCategories();
  $scope.serviceList = ServicesData.getAllServices();
  $scope.businessList = ServicesData.getAllBusinesses();

  // Filter search

  $scope.filterOptions = function(){
    $scope.filteredArray = filterFilter($scope.categoryList, $scope.serviceList, $scope.businessList);
  };
}]);

search.html search.html

<div class="bar bar-subheader" style="border-bottom: 1px solid #ddd; height: 4em">
    <div class="bar bar-header item-input-inset">
      <label class="item-input-wrapper" style="margin-left: -0.5em;">
        <i class="icon ion-ios-search placeholder-icon"></i>
        <input type="search" placeholder="Search" ng-model="data" ng-change="filterOptions()">
      </label>
      <button class="button button-clear">
        Cancel
      </button>
    </div>
  </div>
<div id="result-list" ng-show="data.length > 0" style="margin-top: 3.9em" ng-model="data">
  <ul class="list">
    <li class="item" ng-repeat="item in categoryList | orderBy:'title'">{{item.title}}</li>
    <li class="item" ng-repeat="item in serviceList | orderBy:'title'">{{item.title}}</li>
    <li class="item" ng-repeat="item in businessList | orderBy:'title'">{{item.name}}</li>
  </ul>
</div> 

First you have to annotate you controller correctly. 首先,你必须注释你正确的控制器。

eg: 例如:

.controller('SearchCtrl', ["$scope", "ServicesData", "filterFilter" function($scope, ServicesData, filterFilter)  {

Second filterFilter takes as arguments, one array, an expression to filter that array and a comparator. 第二个filterFilter一个数组,一个用于过滤该数组的表达式和一个比较器作为参数。 Meaning you cannot pass just three arrays and expect some result. 这意味着您不能仅传递三个数组并期望得到一些结果。

If you want to filter all three arrays with one Input you have to call the filter function against each array separately. 如果你想过滤所有三个阵列与一个输入你要拨打单独对每个阵列的过滤功能。 Or apply the filter directly in the ng-repeat of each <li> . 或直接在每个<li>ng-repeat中应用过滤器。

eg: 例如:

<li class="item" ng-repeat="item in filteredCategory = (categoryList | orderBy:'title' | filter:data )">{{item.title}}</li>
<li class="item" ng-repeat="item in filteredService = (serviceList | orderBy:'title' | filter:data)">{{item.title}}</li>
<li class="item" ng-repeat="item in filterredBussiness = (businessList | orderBy:'title' | filter:data)">{{item.name}}</li>

Keep in mind that this way you don't need the ng-change in the input. 请记住,通过这种方式,您不需要在输入中进行ng-change

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM