如何从Angular中的商店获取标签,同时重复另一个商店

时间:2015-10-13 10:10:15

标签: angularjs store ng-repeat

我的控制器中有两家商店:

$scope.categoryStore = [{id: 1, label: "A"}, {id: 2, label: "B"}, {id: 3, label: "C"}];

$scope.collectionStore = [{id: 1, categoryId: 1, name: "Test"}, {id: 1, categoryId: 2, name: "Test2"}];

在我看来,我对collectionStore有一个ng-repeat。现在我想显示categoryStore的标签而不是categoryId。

<tr ng-repeat="item in collectionStore">
   <td>{{item.id}}</td>
   <td>{{item.categoryid}} <!-- this would be the label --></td>
   <td>{{item.name}}</td>
</tr>

1 个答案:

答案 0 :(得分:0)

我的建议是创建一个处理此功能的函数,并在该函数中使用$filter

在您看来:

<tr ng-repeat="item in collectionStore">
   <td>{{item.id}}</td>
   <td>{{getCategoryLabel(item.categoryId)}}</td> <!-- handle this with a function -->
   <td>{{item.name}}</td>
</tr>

然后在你的控制器中:

$scope.getCategoryLabel = function(categoryId) {
    var category = $filter('filter')($scope.categoryStore, { id: categoryId }, true);

    if (category.length) return category[0].label;
}