2014-01-12 40 views
0

我有一個基於Codeigniter的REST API。我用它來提取我的MySQL數據庫爲JSON,並且我想在AngularJS中使用JSON數據。AngularJS處理關係JSON

在數據庫中,我有兩個表;類別和帖子。我在Posts表中保留每個帖子的類別ID。使用PHP,我只需編寫一個處理函數來獲取id作爲參數,並通過連接數據庫來輸出類別名稱。

現在我想在AngularJS中做類似的事情。我創建了兩個JSON對象,一個只保存生日值,另一個保存生日和名字。我想把生日作爲參數,並用ng-repeat輸出名字。我試圖編寫一個自定義過濾器函數,就像這樣;

$scope.custom = function (user) { 
    for (var i = 0; i < $scope.birthdays.length; i++) { 
    if ($scope.birthdays[i].birthday == user.birthday) { 
     return $scope.users[i].name; 
    } 
    } 
}; 

但我想這是一個非常錯誤的方法。那麼這樣做的方式是什麼?還是有辦法?

非常感謝您提前預約。

+1

如果放在一起的jsfiddle或plunker會理解你想要做什麼幫助。然後我們可以幫助你解決這些問題。 –

回答

0

我解決了應用濾波器()使用angular.forEach問題,像{{row.category | custom}}

angular.module('angtestApp') 
.filter('custom', function() { 
    return function (input, $scope) { 
     var name= null; 
     angular.forEach($scope.categories, function(value, key){ 
      if(value.id == input) { 
       name = value.name; 
      } 
     }); 
     return name; 
    } 
});