對於我AngularJS應用程序我有一個NG重複在NG-重複,像這樣:動態過濾
HTML:
<div ng-app="myApp">
<div ng-controller="myController">
<h2>working filter</h2>
<div ng-repeat="category in categories">
<h3>{{category}}</h3>
<div ng-repeat="item in items | filter:{price.red: 0} ">{{item.name}}</div>
</div>
<h2>broken filter</h2>
<div ng-repeat="category in categories">
<h3>{{category}}</h3>
<!-- price[category] should be red, blue, yellow, depending on the category in the ng-repeat -->
<!-- price[category] should be bigger then 0 (or not zero, negative values will not occur) -->
<div ng-repeat="item in items | filter:{price[category]: 0} ">{{item.name}}</div>
</div>
</div>
</div>
的Javascript:
var myApp = angular.module('myApp', []);
myApp.controller('myController', function ($scope) {
$scope.categories = ['red', 'blue', 'yellow'];
$scope.items = [
{name: 'one', price:{red: 0, blue: 1, yellow: 3} },
{name: 'two', price:{red: 2, blue: 0, yellow: 0}},
]
});
請看我的jsFiddle http://jsfiddle.net/hm8qD/
所以我遇到了兩個問題:
- 的flter不接受括號[],所以我不能讓過濾器動態的基礎上,分類
- 過濾器需要返回有價格【類別】大於零的項目。
對於更大的零,我可以做一個自定義過濾器。但據我所知,過濾器不接受參數,所以我無法獲得類別(紅色,藍色或黃色)。
請注意,這是我的實際代碼的簡化版本,這方面可能沒有最好的感覺。我希望我清楚地解釋我需要過濾器做什麼,因爲我是AngularJS的新手。
你可以將你的嘗試轉儲到jsFiddle嗎? –
過濾器的語法:'item in items |過濾:filterCriteria' – TheHippo
是小提琴,請 –