Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
1  
apps[0].genre should be apps.genre –  dandavis Jan 22 '14 at 3:25
    
wow, thanks.. it work to me. both of them.. :)) –  tuanpembual Jan 22 '14 at 3:36

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
    
This works perfect. Thank You.. :) –  tuanpembual Jan 22 '14 at 3:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.