简体   繁体   English

如何按angularJS中的object属性进行过滤

[英]How to filter by object property in angularJS

I am trying to create a custom filter in AngularJS that will filter a list of objects by the values of a specific property. 我试图在AngularJS中创建一个自定义过滤器,它将按特定属性的值过滤对象列表。 In this case, I want to filter by the "polarity" property(possible values of "Positive", "Neutral", "Negative"). 在这种情况下,我想过滤“极性”属性(可能的值为“正”,“中性”,“否定”)。

Here is my working code without the filter: 这是我没有过滤器的工作代码:

HTML: HTML:

<div class="total">
    <h2 id="totalTitle"></h2>
    <div>{{tweets.length}}</div>
    <div id="totalPos">{{tweets.length|posFilter}}</div>
    <div id="totalNeut">{{tweets.length|neutFilter}}</div>
    <div id="totalNeg">{{tweets.length|negFilter}}</div>
</div>

Here is the "$scope.tweets" array in JSON format: 这是JSON格式的“$ scope.tweets”数组:

{{created_at: "Date", text: "tweet text", user:{name: "user name", screen_name: "user       screen name", profile_image_url: "profile pic"}, retweet_count: "retweet count", polarity: "Positive"},
 {created_at: "Date", text: "tweet text", user:{name: "user name", screen_name: "user screen name", profile_image_url: "profile pic"}, retweet_count: "retweet count", polarity: "Positive"},
 {created_at: "Date", text: "tweet text", user:{name: "user name", screen_name: "user screen name", profile_image_url: "profile pic"}, retweet_count: "retweet count", polarity: "Positive"}}

The best filter I could come up with as follows: 我能想出的最好的过滤器如下:

myAppModule.filter('posFilter', function(){
return function(tweets){
    var polarity;
    var posFilterArray = [];
    angular.forEach(tweets, function(tweet){
        polarity = tweet.polarity;
        console.log(polarity);
        if(polarity==='Positive'){
              posFilterArray.push(tweet);
        }
        //console.log(polarity);
    });
    return posFilterArray;
};
});

This method returns an empty array. 此方法返回一个空数组。 And nothing is printed from the "console.log(polarity)" statement. 并且没有从“console.log(极性)”声明中打印出任何内容。 It seems like I am not inserting the correct parameters to access the object property of "polarity." 看来我没有插入正确的参数来访问“极性”的对象属性。

Any ideas? 有任何想法吗? Your response is greatly appreciated. 非常感谢您的回复。

You simply have to use the filter filter (see the documentation ) : 您只需使用filterfilter器(请参阅文档 ):

<div id="totalPos">{{(tweets | filter:{polarity:'Positive'}).length}}</div>
<div id="totalNeut">{{(tweets | filter:{polarity:'Neutral'}).length}}</div>
<div id="totalNeg">{{(tweets | filter:{polarity:'Negative'}).length}}</div>

Fiddle 小提琴

The documentation has the complete answer. 文档有完整的答案。 Anyway this is how it is done: 无论如何,这是如何做到的:

<input type="text" ng-model="filterValue">
<li ng-repeat="i in data | filter:{age:filterValue}:true"> {{i | json }}</li>

will filter only age in data array and true is for exact match. 将仅过滤data数组中的age ,而true则用于完全匹配。

For deep filtering, 对于深度过滤,

<li ng-repeat="i in data | filter:{$:filterValue}:true"> {{i}}</li>

The $ is a special property for deep filter and the true is for exact match like above. $是深度过滤器的特殊属性, true用于完全匹配,如上所述。

You could also do this to make it more dynamic. 您也可以这样做,使其更具动态性。

<input name="filterByPolarity" data-ng-model="text.polarity"/>

Then you ng-repeat will look like this 然后你的重复将看起来像这样

<div class="tweet" data-ng-repeat="tweet in tweets | filter:text"></div>

This filter will of course only be used to filter by polarity 该滤波器当然仅用于按极性滤波

We have Collection as below: 我们有如下收藏:


在此输入图像描述

Syntax: 句法:

{{(Collection/array/list | filter:{Value : (object value)})[0].KeyName}}

Example: 例:

{{(Collectionstatus | filter:{Value:dt.Status})[0].KeyName}}

-OR- -要么-

Syntax: 句法:

ng-bind="(input | filter)"

Example: 例:

ng-bind="(Collectionstatus | filter:{Value:dt.Status})[0].KeyName"

You can try this. 你可以试试这个。 its working for me 'name' is a property in arr. 它为我工作'名字'是arr的财产。

repeat="item in (tagWordOptions | filter:{ name: $select.search } ) track by $index

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

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