指令的角度过滤器内容

时间:2016-02-23 18:35:08

标签: angularjs

我希望能够通过数据JSON中的"cat":属性过滤自定义自定义元素的ng-repeat内容。我对Angular指令有基本经验,但你可以说我的理解远非精通。我有一种感觉,问题在于我的范围{}和cat属性的指令,但我对如何继续进行了一些调整。目前我的HTML就像这样:

HTML:

<x-filtered-repeat cat="characters | filter : {cat: 'jedi'} : true | filter:query |orderBy: orderList"></x-filtered-repeat>

Template filtered-repeat.html:

<div>
    <ul>
      <li data-ng-repeat="character in characters">
        <h2>{{character.name}}</h2>
      </li>
    </ul>
    ...
</div>

JS:

.directive('filteredRepeat', function() {
  return {
    restrict: 'E',
    scope: {
      cat: '='
},
    templateUrl: 'partials/filtered-repeat.html'
  };
});

JS文件中的JSON:

$scope.businesses = [
    {"name": "Obi-Wan Kenobi",
     "index":88,
      "cat": "jedi"},
    {"name": "Yoda",
     "index":69,
      "cat":"jedi"},
    {"name": "Lando",
     "index":31,
      "cat": "smuggler"},
    {"name": "Han Solo",
     "index":90,
      "cat": "smuggler"},
    {"name": "Darth Vader",
     "index":98,
      "cat": "sith"},
    {"name": "Jar-Jar Binks",
     "index":80,
      "cat": "alien"},
    {"name": "Mace Windu",
     "index":45,
      "cat": "jedi"},
    {"name": "Chewy",
     "index":76,
      "cat": "smuggler"}
  ];

1 个答案:

答案 0 :(得分:0)

在指令模板中

尝试使用过滤后的数组:

   <li data-ng-repeat="character in cat">
        <h2>{{character.name}}</h2>
   </li>