讓我們假設一個假設的情況。您和我有一個由選擇器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
,但也產生了錯誤。
的文件清楚地表明,「屬性的指令,改變元素的外觀或行爲,組件,或其他指令。」,但我不明白的指令如何獲取訪問組件它修改。
任何人都可以提供幫助嗎?