2017-02-18 58 views
0

時,我有following plunker如何訪問ElementRef陰影DOM內使用NG2機視封裝

我遇到的問題是,當我打開機視封裝我再也不能訪問內部DOM(由設計)。我的問題是,Angular 2是否提供了一種方法來訪問組件中的DOM陰影。

// Shows 1 
platformBrowserDynamic().bootstrapModule(AppModule) 
// Shows 0 
// platformBrowserDynamic().bootstrapModule(AppModule, {defaultEncapsulation: ViewEncapsulation.Native}) 

回答

2

與Angular2沒有任何關係。您可以使用shadowRoot屬性來搜索Shadow DOM中的元素。

const shadowRoot: DocumentFragment = this.element.nativeElement.shadowRoot; 
this.count = shadowRoot.children.length; 

或者使用ViewChild/ViewChildrenContentChild/ContentChildren獲得參考ElementRef

+0

使用@ViewChild似乎工作的感謝https://plnkr.co/edit/4XIjpQCAHnRr30bt0pHN?p=preview。現在讓我們看看在使用pug時是否可以使用本地ID :-) – Jackie