3

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

2
  • 1
    apps[0].genre should be apps.genre Commented Jan 22, 2014 at 3:25
  • wow, thanks.. it work to me. both of them.. :)) Commented Jan 22, 2014 at 3:36

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

why if you do console.log(apps)the result is 4 object (2 for first row and 2 for second one)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.