我一直在努力制作一份地理位置列表,每种都有税收(并非所有税收都适用于所有地理位置).
因此,我对geozones进行了ng-repeat,并在每个内部进行了所有税收的ng-repeat.问题是我不知道如何发送此时正在过滤的geozone的id.这是现在的代码:
<md-option ng-repeat="tax in taxClasses | filter: filterTax" value="{{ '{{ tax.id }}' }}">{{ '{{ tax.name }}' }}</md-option>
和JS:
$scope.filterTax = function(tax,n){ angular.forEach(tax.geoZone,function(geo){ if(geo === $scope.prices[n].geozoneId){ return true; } }); return false; };
需要n作为geozone的索引,或类似的东西.提前致谢!
解决方法
你的想法并不遥远,但使用过滤器:甚至不需要管道|已经是一个过滤命令:
ng-repeat="<var> in <array> | <filterFunction>:<...arguments>"
因此,您可以创建一个过滤器(有关详细信息,请参阅https://docs.angularjs.org/guide/filter)
ng-repeat="tax in taxClasses | filterTax: <geozoneIndex>"
集合中的值将作为filterTax函数的第一个参数传递.任何进一步的参数都以冒号分隔:.
当你使用它时,你必须传播这样的过滤方法:
app.module(...).filter('filterTax',function(/* injected services */) { return function (/* arguments */ input,geozoneIndex) { // function body } });
或者使用范围中的过滤函数:
// template ng-repeat="tax in filterTaxes(taxClasses,<geozoneIndex>)" // script $scope.filterTaxes = function(taxClasses,geozoneIndex) { // do the whole filtering,but return an array of matches return taxClasses.filter(function(taxClass) { return /* in or out code */; }); };
这意味着您的geozoneIndex可以是固定值,也可以从该范围内可用的变量中提取.