我想要实现的是基于哪个组件的父组件的不同行为。所以在以下两种情况下,孩子的行为会略有不同。Angular 2子组件检查父组件
例如一个
<parent-a>
<child></child>
</parent-a>
例B
<parent-b>
<child></child>
</parent-b>
我试着做以下
constructor(
el: ElementRef) {
this.el = el;
}
ngAfterViewInit() {
const hostElem = this.el.nativeElement;
console.log(hostElem);
console.log(hostElem.children);
console.log(hostElem.parentNode);
}
eventhough ngAfterViewInit是有点晚了我,我还有空的parentNode
有什么办法可以实现这样的事情吗?
这不是它应该如何工作。相反,将来自父母的输入传递给孩子,告诉其如何行事。 –
是啊,它已经这样做(与服务),但如果父母不是我期望的那种? (我基本上建立一个Api)。我基本上想给错误/警告与预期的父节点列表 – elasticrash