简体   繁体   English

如何在递归指令中访问父母?

[英]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 : 我使用https://github.com/dotJEM/angular-tree作为递归指令来遍历这种模型:

  $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 > ... 例如: 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)? 如果我没有记错的话,我可以使用$ parent来获取每个节点的父节点...但是我将如何获取每个父节点呢?

I have created a plnkr to illustrate : http://plnkr.co/edit/DOc9k4jT9iysJLFvvg3u?p=preview 我创建了一个plnkr来说明: 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)}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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