Equivalent of $compile in Angular 2

后端 未结 9 854
名媛妹妹
名媛妹妹 2020-11-21 06:50

I want to manually compile some HTML containing directives. What is the equivalent of $compile in Angular 2?

For example, in Angular 1, I could dynamic

9条回答
  •  广开言路
    2020-11-21 07:04

    In order to dinamically create an instance of a component and attach it to your DOM you can use the following script and should work in Angular RC:

    html template:

    Loader component

    import { Component, DynamicComponentLoader, ElementRef, Injector } from '@angular/core';
    import { WidgetMeteoComponent } from './widget-meteo';
    import { WidgetStatComponent } from './widget-stat';
    
    @Component({
      moduleId: module.id,
      selector: 'widget-loader',
      templateUrl: 'widget-loader.html',
    })
    export class WidgetLoaderComponent  {
    
      constructor( elementRef: ElementRef,
                   public dcl:DynamicComponentLoader,
                   public injector: Injector) { }
    
      viewMeteo() {
        this.dcl.loadAsRoot(WidgetMeteoComponent, '#container', this.injector);
      }
    
      viewStats() {
        this.dcl.loadAsRoot(WidgetStatComponent, '#container', this.injector);
      }
    
    }
    

提交回复
热议问题