如何使用过滤AngularJs JSON数据 [英] How to filter JSON Data with AngularJs
问题描述
我有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屋!