过滤器阵列内的物体角度

时间:2015-08-05 10:35:33

标签: javascript 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:

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

data.keywords来自这里:

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

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

因为我需要从网址中的ID显示第一批结果。

1 个答案:

答案 0 :(得分:0)

取自angularjs文件:

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

模式对象可用于过滤数组

包含的对象的特定属性      

请注意,命名属性将匹配同一级别的属性   只有,而特殊的$属性将匹配相同的属性   等级或更深。

查看expression部分: filter documentation

<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
        }, ],
    }]
};
}]);

请参阅工作示例:https://jsfiddle.net/1j5fnrd6/

输入“我的名字”,你会看到过滤器被应用于随机的东西2