简体   繁体   English

使用NG2 Native View Encapsulation时,如何访问shadow-dom内的ElementRef

[英]How do I access the ElementRef inside shadow-dom when using NG2 Native View Encapsulation

I have the following plunker 我有以下的plunker

The problem I am having is when I turn on native view encapsulation I can no longer access the internal DOM (by design). 我遇到的问题是当我打开本机视图封装时,我无法再访问内部DOM(按设计)。 My question is, does Angular 2 provide a way to access the DOM INSIDE the shadow dom from the component. 我的问题是,Angular 2是否提供了一种从组件访问阴影dom中的DOM的方法。

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

There is nothing to do with Angular2. 与Angular2无关。 You can use shadowRoot property to search elements inside of Shadow DOM. 您可以使用shadowRoot属性搜索Shadow DOM内的元素。

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

Or use ViewChild/ViewChildren ContentChild/ContentChildren to get reference to ElementRef 或者使用ViewChild/ViewChildren ContentChild/ContentChildren来获取对ElementRef引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM