簡體   English   中英

如何在遞歸指令中訪問父母?

[英]How to access parents in recursive directive?

我使用https://github.com/dotJEM/angular-tree作為遞歸指令來遍歷這種模型:

  $scope.model = [
      {
          label: 'parent1',
          children: [{
              label: 'child'
          }]
      }, 
      {
          label: 'parent2',
          children: [{
              label: 'child',
              children: [{
                  label: 'innerChild'
              }]
          }]
      }, 
      {
          label: 'parent3'
      }
  ];

在模板中,代碼如下所示:

<div data-dx-start-with="model">
    <div data-ng-repeat="node in $dxPrior">
        <a class="list-group-item">
            <span class="icon" data-ng-click="toggle(node)"><i class=" icon ion-android-arrow-dropdown"></i>&nbsp;</span>
            <span>{{ node.label}} ({{node.children.length}})</span>
        </a>
        <ul data-ng-show="node.expanded" data-dx-connect="node.children"></ul>
    </div>
</div>


現在,我將如何訪問每個父母? 我希望能夠建立樹狀視圖的面包屑。

例如: parent2> child> innerChild> ...

如果我沒有記錯的話,我可以使用$ parent來獲取每個節點的父節點...但是我將如何獲取每個父節點呢?

我創建了一個plnkr來說明: http ://plnkr.co/edit/DOc9k4jT9iysJLFvvg3u?p=preview

只需對其編程。 使函數可以遍歷父母直至到達根,並在每個步驟中構造父母數組:

$scope.getParentsBreadcrumb = function (thisScope) {
  var parents = [];
  while (thisScope && thisScope.node) {
    parents.unshift (thisScope.node.label);
    thisScope = thisScope.$parent.$parent;
  }
  return parents.join(' > ')
}

然后在您要打印面包屑的模板中調用它:

{{getParentsBreadcrumbs (this)}}

暫無
暫無

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

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