如何封装在角2动态地添加元素 [英] How to encapsulate dynamically added elements in Angular 2
本文介绍了如何封装在角2动态地添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图让MDL与Angular2工作。
Trying to make MDL working with Angular2.
使用DOM影子角仿真封装所有CSS和HTML它从code斩获。但是当我使用 componentHandler.upgradeElement()
,它创建了无封装新的元素。
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?
推荐答案
添加这个简单的指令MDL元!
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);
}
}
这篇关于如何封装在角2动态地添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文