简体   繁体   English

用过滤器更新ng-repeat

[英]update ng-repeat with filter

I have some angular code that I am working with and hopefully soon refactoring. 我有一些正在使用的角度代码,希望很快能重构。
I am trying to filter through a ng-repeat based off of a checkbox. 我正在尝试根据复选框来过滤ng-repeat

I have a data tree that looks like so ... 我有一个看起来像这样的数据树...

{
    name: 'mail',
    rating: 2.4,
    source: 'EDD',
    users: 1000,
    description: ''
}

The ng-repeat work is set up to iterate through 设置ng-repeat工作以迭代遍历

<li ng-repeat="rule in property">{{rule[entityPropertyName]}</li>

The property was renamed off of a products service that exists in the searchController like so 将该属性重命名为searchController中存在的产品服务,如下所示

HTML 的HTML

<search-section productName="qualifier" 
                product="qualifiers" 
                entityPropertyName="name"></search-section>

Directive 指示

controller: 'searchController',
scope: {
    product: '=',
    productName: '@',
    entityPropertyName: '@'
},

I am filtering through the checkbox with a simple function in the controller 我正在使用控制器中的简单功能通过复选框进行过滤

$scope.filterAdded = function(sourceName) {
    if($scope.filteredQualifiers.sourceName[sourceName]) {
        return sourceName;
    }
}  // returns source name 

The source name is returned ex: media , or age . 返回源名称,例如: mediaage

I was hopeful that I could add 我希望我可以补充

$scope.filterAdded 

to the ng-repeat iterator like so 像这样到ng-repeat迭代器

<li ng-repeat="rule in property | filter: filterAdded">{{rule[entityPropertyName]}</li>

This filter would be supported from the directive like so 像这样的指令将支持此过滤器

scope: {
    entities: '=',
    entityName: '@',
    entityPropertyName: '@',
    filterAdded: '&'
},

With filterAdded being seen as a function. 随着filterAdded被视为一个功能。

With filter added returning a single value like EDD 添加过滤器后,返回单个值,如EDD

I identified that I that I would need to filter through the rule source. 我确定自己需要过滤规则源。

source: 'EDD'

Not seeing a way that I could compare get rule.source and the filterAdded value. 没有看到可以比较get rule.source和filterAdded值的方法。

<li ng-repeat="rule in property | filter: filterAdded"></li> 

Any insight or feedback is appreciated. 任何见解或反馈表示赞赏。

I'm into Angular 4, so I will use this language but you can adapt to angular js (1.x). 我将学习Angular 4,因此我将使用这种语言,但您可以适应angular js(1.x)。

If I got it right, would use the ngFor or ngRepeat with the ngIf instead of using pipes. 如果我做对了,可以将ngFor或ngRepeat与ngIf一起使用,而不要使用管道。

<ul *ngFor=" let rule in property">

*note that the ngFor would be used for the ul, not the li. *请注意,ngFor将用于ul,而不是li。

Then, on li tag I would use the ngIf to check if it is under a certain filter. 然后,在li标签上,我将使用ngIf检查它是否在某个过滤器下。 On the ngIf you can bind to a function and even pass properties of the object or items of the array you are iterating like: 在ng上,如果您可以绑定到一个函数,甚至可以传递对象或数组项的属性,则可以像这样进行迭代:

<li *ngIf="isThisOnFilter(rule['rating'])">{{ rule['name'] }}</li>

Hope it helps. 希望能帮助到你。

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

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