2014-01-22 91 views
3

我有json數據,我希望它通過過濾器如流派顯示。 最後一個問題從How to filter JSON-Data with AngularJs?不工作給我。如何過濾JSON數據與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> 

做我丟失somethink? 謝謝..

+1

apps [0] .genre應該是apps.genre – dandavis

+0

哇,謝謝.. 它適用於我。他們兩個.. :)) –

回答

8

你不需要一個比較器功能來做那個過濾。您可以使用此:

<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

+0

這工作完美。謝謝.. :) –

+0

爲什麼如果你做console.log(應用程序)結果是4個對象(第一行2和第二行2) –