Angular7父组件?

时间:2018-12-17 20:11:06

标签: angular typescript

我正在尝试为我的Web应用程序设置安全模型,此安全模型将具有“面板”(节点),每个节点将具有子级,如果顶层面板没有正确的权限,则所有子级面板应该被拒绝访问。我现在正在努力的部分是从父元素获取组件。角度应用程序一旦编译就出现了,代码不再了解实际组件了吗?这是一些代码,我应该打上this.ParentPanel = e;行,但是这永远不会发生,因为instaceof总是返回false。

import { Component, OnInit, Input, ElementRef} from '@angular/core';
import { TESTControl } from '../testcontrol/testcontrol.component';

@Component({
  selector: 'test-panel',
  templateUrl: './testpanel.component.html',
  styleUrls: ['./testpanel.component.scss']
})

export class TESTPanelComponent implements OnInit {

  @Input() disabled = false; // when this is disabled, all children will get disabled as well.

  public Children: TESTControl;
  public ParentPanel: any;

  ngOnInit() {
    this.GetParentPanel();
  }

  constructor(public element: ElementRef) {

  }

  GetParentPanel() {
    // Step up the dom to find the next object that is the parent and a panel (If any)
    let stepping = true;
    let e = this.element.nativeElement;
    let override = 0;
    while (stepping) {
      e = e.parentElement;
      console.log('e:', e);
      if (e) {
        if (e instanceof TESTPanelComponent) {
          stepping = false;
          this.ParentPanel = e;
          console.log('parent panel set to : ' , this.ParentPanel);
        }
      } else {
        stepping = false;
        console.log('no parent');
      }
      override ++;
      if (override >= 1000) { console.log('override. Doesnt exist.'); stepping = false; }
    }
  }

}

谢谢。

0 个答案:

没有答案