如何使用 angularjs 使用下拉选择的索引过滤列表?

How to filter a list using the dropdown selected index using angularjs?

我有一个数据列表,并根据类别对数据进行了分组。在显示该类别名称的下拉列表中。当我 select 一个特定的类别时,它将只显示该类别列表。如果我 select "All Category" 那么它将显示所有类别列表。
请看下面的link和代码。

http://plnkr.co/edit/gNKQpUT2OwtFkxxyTwPY

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
$scope.locationList = [
{
"tid": "2440",
"name": "Alfredo's Pizzeria",
"field_location_category": [
  "Food and Dining"
]
},
{
"tid": "2441",
"name": "Allegro Dining Room",
"field_location_category": [
  "Food and Dining"
]
},
{
"tid": "2443",
"name": "Art Gallery",
"field_location_category": [
  "Gifts & Memories"
]
},
{
"tid": "2435",
"name": "Bellini's",
"field_location_category": [
  "Entertainment/Bars"
]
},
{
"tid": "2498",
"name": "Bullseye",
"field_location_category": [
  "Pools, Sports & Spa"
]
}
];
var indexedloc = [];
        $scope.locationListToFilter = function(){
            indexedloc = [];
            return $scope.locationList; 
        }

        $scope.filterLocation = function(Loc){
            var locationIsNew = indexedloc.indexOf(Loc.field_location_category[0]) == -1;
            if(locationIsNew){
                indexedloc.push(Loc.field_location_category[0]);                    
            }
            return locationIsNew;
        }   
        $scope.returnFilterLoc = function(){return indexedloc};
    });

HTML

 <body ng-controller="MainCtrl">
<select id="category_select" ng-options="loc as loc for loc in returnFilterLoc()" ng-model="locationList">
    <option value="">All Categories</option>
</select>
<nav id="entertainment_bars" class="persist-area content-list l2g-menu-list" ng-repeat="loc in locationListToFilter() | filter:filterLocation">
  <h3 class="persist-header">{{loc.field_location_category[0]}}</h3>
  <ul>
    <li ng-repeat="Loc in locationList | filter:{field_location_category:loc.field_location_category[0]}">
    <span>{{Loc.name}}</span> </li>
  </ul>
</nav>

请帮助我。提前致谢。

我分叉了你的plunker

控制器

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.locationList = [
  {
    "tid": "2440",
    "name": "Alfredo's Pizzeria",
    "field_location_category": [
      "Food and Dining"
    ]
  },
  {
    "tid": "2441",
    "name": "Allegro Dining Room",
    "field_location_category": [
      "Food and Dining"
    ]
  },
  {
    "tid": "2443",
    "name": "Art Gallery",
    "field_location_category": [
      "Gifts & Memories"
    ]
  },
  {
    "tid": "2435",
    "name": "Bellini's",
    "field_location_category": [
      "Entertainment/Bars"
    ]
  },
  {
    "tid": "2498",
    "name": "Bullseye",
    "field_location_category": [
      "Pools, Sports & Spa"
    ]
  }
];
  var indexedloc = [];
  $scope.categories = [];
  angular.forEach($scope.locationList, function(item){
    if($scope.categories.indexOf(item.field_location_category[0]) === -1){
      $scope.categories.push(item.field_location_category[0]); 
    }
  });

    $scope.locationListToFilter = angular.copy($scope.locationList);

    $scope.filterLocation = function(Loc){
      $scope.locationListToFilter.length = 0;
      if($scope.selectedCategory){
        angular.forEach($scope.locationList,function(item){
        if(item.field_location_category[0] === $scope.selectedCategory){
            $scope.locationListToFilter.push(item);
        }
        });
      }else{
        Array.prototype.push.apply($scope.locationListToFilter, $scope.locationList);
      }
    }   
});

HTML

<body ng-controller="MainCtrl">
    <select id="category_select" ng-options="loc as loc for loc in categories" ng-model="selectedCategory"
    ng-change="filterLocation()">
        <option value="">All Categories</option>
    </select>
    <nav id="entertainment_bars" class="persist-area content-list l2g-menu-list" ng-repeat="loc in locationListToFilter">
      <h3 class="persist-header">{{loc.field_location_category[0]}}</h3>
      <ul>
        <li ng-repeat="Loc in locationList | filter:{field_location_category:loc.field_location_category[0]}">
        <span>{{Loc.name}}</span> </li>
      </ul>
    </nav>
  </body>