让我们假设一个假设的情况。您和我有一个由选择器a-component
标识的组件(名为AComponent),以及由选择器[is-modified]
标识的指令。如何访问由Angular2中的指令修改的组件?
在另一个组件的定义文件,我们可以使用下面的模板,结合我们的组件和我们的指令,它会修改组件:
<a-component is-modified></a-component>
的documentation for a attribute Directive表明构造给人以ElementRef指令访问,但是没有从ElementRef到组件父节点的链接。
export class IsModifiedDirective
{
constructor(elementReference : ElementRef)
{
console.log(elementReference);
//no connection appears to exist between elementReference and the component
//also: ElementRef has security risks, so it should be avoided.
debugger;
}
}
我试图使用喷射来喷射所需的部件,并改变了ElementRef
到ComponentRef<AComponent>
;这给出了ComponentRef没有指定供应商的错误。然后我尝试注入组件AComponent
,但也产生了错误。
的文件清楚地表明,“属性的指令,改变元素的外观或行为,组件,或其他指令。”,但我不明白的指令如何获取访问组件它修改。
任何人都可以提供帮助吗?