【问题标题】:How to filter JSON Data with AngularJs如何使用 AngularJs 过滤 JSON 数据
【发布时间】:2014-01-22 03:22:52
【问题描述】:

我有 json 数据,我希望它通过流派等过滤器显示。 How to filter JSON-Data with AngularJs? 的最后一个问题对我不起作用。

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

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

我想念什么吗? 谢谢你。。

【问题讨论】:

  • apps[0].genre 应该是 apps.genre
  • 哇,谢谢.. 它对我有用。他们俩.. :))

标签: javascript json angularjs


【解决方案1】:

您不需要比较器功能来进行过滤。你可以使用这个:

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

请注意,我使用app.name 来显示ngRepeat 生成的当前应用程序。 apps[0].name 将重复显示第一个条目(如果多个条目与您的过滤器匹配)。

demo fiddle

如果您想使用比较器功能,这将起作用(每次发送一个对象,因此您不想将其作为数组引用):

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

comparator demo

【讨论】:

  • 为什么如果你做 console.log(apps) 结果是 4 个对象(第一行 2 个,第二行 2 个)
猜你喜欢
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
相关资源
最近更新 更多