首页 > 解决方案 > Angular7父组件?

问题描述

我正在尝试为我的 webapp 设置安全模型,此安全模型将具有“面板”(节点),每个节点都会有子节点,如果顶级面板没有正确的权限,则应拒绝所有子面板使用权。我现在正在努力的部分是从父元素中获取组件。一旦编译了角度应用程序,代码就不再知道实际的组件?这是一些代码,我应该打那一行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; }
    }
  }

}

谢谢。

标签: angulartypescript

解决方案


推荐阅读