简体   繁体   English

对象角度内部的过滤器数组

[英]filter array inside object angular

I'm trying to filter the results by term but since it's inside an array it's being quite hard to reach them with the filters in AngularJS. 我正在尝试按术语过滤结果,但是由于它在array因此使用AngularJS中的过滤器很难达到它们。

var test=[

{

"title": "random stuff",

"path": "/random_stuff.html",

"labels": [

  {
    "term": "viewed",
    "probability": 0.083
  },

  {
    "term": "firefox",
    "probability": 0.083
  },

  {
    "term": "cookies",
    "probability": 0.083
  },

  {
    "term": "times",
    "probability": 0.055
  },

],

}
];

HTML : HTML:

<input class="looking" type="text"  ng-model="data.keywords"/>
<li ng-repeat="se in searchCtrl.data.pages | orderBy : '-views' | filter: ??" >

the data.keywords come from here: data.keywords来自这里:

var id=$routeParams.teamId;
var context=this;

context.data = {
    pages:[],
    keywords:id,
    isLogged:false
};

Because I need the first results to be showed from the ID in the URL. 因为我需要从URL中的ID显示第一个结果。

Taken from the angularjs documentation: 取自angularjs文档:

friendObj in friends | filter:{$: 'your filter'}

A pattern object can be used to filter specific properties on objects contained by array 模式对象可用于过滤数组包含的对象的特定属性

Note that a named property will match properties on the same level only, while the special $ property will match properties on the same level or deeper. 请注意,命名属性将仅匹配同一级别的属性,而特殊的$属性将匹配同一级别或更深级别的属性。

View the expression section: filter documentation 查看expression部分: 过滤器文档

<div ng-controller="MyCtrl">
    <input type="text" data-ng-model="search" />
    <ul>
    <li data-ng-repeat="page in list.pages | filter: {$: search}">{{page.title}}</li>
    </ul>
</div>

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', ['$scope', function ($scope) {
  $scope.list = {
    pages: [{
        "title": "random stuff1",
            "path": "/random_stuff.html",
            "labels": [{
            "term": "viewed",
                "probability": 0.083
        }, {
            "term": "firefox",
                "probability": 0.083
        },

        {
            "term": "cookies",
                "probability": 0.083
        }, {
            "term": "times",
                "probability": 0.055
        }, ],
    }, {
        "title": "random stuff2",
            "path": "/random_stuff.html",
            "labels": [{
            "term": "viewed",
                "probability": 0.083
        }, {
            "term": "firefox",
                "probability": 0.083
        },

        {
            "term": "cookies",
                "probability": 0.083
        }, {
            "term": "my name",
                "probability": 0.078
        }, ],
    }]
};
}]);

Please see working example: https://jsfiddle.net/1j5fnrd6/ 请参见工作示例: https : //jsfiddle.net/1j5fnrd6/

Type 'my name' and you will see the filter being applied on random stuff2 输入“我的名字”,您会看到该过滤器已应用于随机的东西2

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

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