簡體   English   中英

如何使用AngularJS ng-repeat打破值?

[英]How do I break on values using AngularJS ng-repeat?

我希望我能解釋一下我的目標。

我正在從API中檢索項目列表。 為簡單起見,我們假設這是返回的JSON:

[ { id: 1, name: "Project 1", pm: "msmith"}, 
  {id: 2, name: "Project 2", pm: "msmith"},
  {id: 3, name: "Project 3", pm: "msmith"},
  {id: 4, name: "Project 4", pm: "sjones"},
  {id: 5, name: "Project 5", pm: "sjones"} ]

項目列表將按照下午按名稱排序。 我希望項目顯示在這樣的頁面上:

msmith (3 projects)
    Project 1
    Project 2
    Project 3

sjones (2 projects)
    Project 4
    Project 5

我怎么能在AngularJs中做到這一點?

因此,您需要一些功能來獲取不同pms的列表

<div ng-repeat="pm in distinctPms">
  {{pm}}
  <ul>
      <li ng-repeat="name in items | filter:{pm: pm}">
          {{name.name}}
      </li>
  </ul>
</div> 

function SimpleController($scope) {

  function distinctPm() {
    var unique= [];
    for (var i = 0; i < $scope.items.length; i++) {
        if (unique.indexOf($scope.items[i].pm) == -1) {
            unique.push($scope.items[i].pm)
        }
    }
    return unique;
  }

  $scope.items = [ 
    {id: 1, name: "Project 1", pm: "msmith"}, 
    {id: 2, name: "Project 2", pm: "msmith"},
    {id: 3, name: "Project 3", pm: "msmith"},
    {id: 4, name: "Project 4", pm: "sjones"},
    {id: 5, name: "Project 5", pm: "sjones"}
  ];

  $scope.distinctPms = distinctPm();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM