简体   繁体   中英

How to access parents in recursive directive?

I'm using https://github.com/dotJEM/angular-tree as a recursive directive to loop through this kind of model :

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

In the template, the code looks like this:

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


Now, how would I go about accessing each parent? I want to be able to build a breadcrumb of the treeview.

Ex : parent2 > child > innerChild > ...

I can get each node's parent by using $parent if I'm not mistaken... but how would I go about getting each parent(s)?

I have created a plnkr to illustrate : http://plnkr.co/edit/DOc9k4jT9iysJLFvvg3u?p=preview

Just program it. Make function that will walk thru parents until reach the root and construct array of parents in each step:

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

Then call it in your template where you want to print breadcrumbs:

{{getParentsBreadcrumbs (this)}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM