【问题标题】:How get a label from a store in Angular while repeat another store如何在重复另一家商店的同时从 Angular 中的商店获取标签
【发布时间】:2015-12-16 20:16:05
【问题描述】:

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

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

【问题讨论】:

    标签: angularjs store ng-repeat


    【解决方案1】:

    我的建议是创建一个函数来处理这个问题并在该函数中使用$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;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-18
      • 2021-11-21
      • 2016-03-04
      • 2015-09-04
      • 1970-01-01
      • 2021-10-21
      • 2016-12-16
      • 2011-07-11
      • 1970-01-01
      相关资源
      最近更新 更多