如何使用过滤AngularJs JSON数据 [英] How to filter JSON Data with AngularJs

查看:174
本文介绍了如何使用过滤AngularJs JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JSON数据,我希望它通过过滤器显示如流派。
最后一个问题如何使用AngularJs JSON-数据过滤器?不工作在我身上。

i have json data, and i want it show by filter such as genre. Last question from How to filter JSON-Data with AngularJs? dont work to me.

myapp.js

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

myApp.controller('myApp', function($scope) { 

$scope.isoffice = function(apps) {
    return apps[0].genre === "office";
};

$scope.apps = [
    {
  "id": "stardict",
  "genre": "aksesoris", 
  "name": "Stardict"
},
{
  "id": "libreoffice",
  "genre": "office", 
  "name": "LibreOffice"
}];
}

我的index.html

my index.html

<div ng-repeat="app in apps | filter:isoffice">
  {{apps[0].name}}
</div>

我失踪somethink?
谢谢..

do i missing somethink? thank you..

推荐答案

您不需要比较器功能做过滤。您可以使用此:

You don't need a comparator function to do that filtering. You can use this:

<div ng-repeat="app in apps | filter:{genre:'office'}">
  {{app.name}}
</div>

请注意,我用 app.name 来显示由 ngRepeat 产生的电流应用。 应用[0]。名称将反复显示的第一个条目(如果有多个条目匹配过滤器)。

Note that I use app.name to display the current app generated by ngRepeat. apps[0].name will repeatedly display the first entry (if multiple entries match your filter).

演示小提琴

如果你想用一个比较器功能,这将工作(一个对象在每次发送,所以你不想引用它作为一个数组):

If you want to use a comparator function, this will work (one object is sent in each time so you don't want to reference it as an array):

$scope.isoffice = function(apps) {
   return apps.genre === "office";
};

比较演示

这篇关于如何使用过滤AngularJs JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆