How to encapsulate dynamically added elements in Angular 2?

北城余情 提交于 2019-12-10 17:56:03

问题


Trying to make MDL working with Angular2.

With shadow DOM emulation angular encapsulates all css and html which it gains from code. But when I use componentHandler.upgradeElement(), it creates new elements with no encapsulation.

What should I do to make it encapsulated too?


回答1:


Adding this simple directive mdl element!

import {Component, ElementRef } from 'angular2/core';
declare var componentHandler: any;


@Component({
    selector: '[mdl]',
    template: `<ng-content></ng-content>`
})



    export class MdlComponent {

    constructor(public el: ElementRef) {

        MdlComponent.mdlWrapper(el);

    }

    static mdlWrapper(element: ElementRef) {

        componentHandler.upgradeElement(element.nativeElement);
    }
}


来源:https://stackoverflow.com/questions/33386129/how-to-encapsulate-dynamically-added-elements-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!