Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter JSON Data with AngularJs

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"
}];
}

my index.html

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

do i missing somethink? thank you..

like image 272
Estu Fardani Avatar asked Dec 28 '25 15:12

Estu Fardani


1 Answers

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>

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).

demo fiddle

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";
};

comparator demo

like image 189
KayakDave Avatar answered Dec 31 '25 06:12

KayakDave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!