2016-11-12 10 views
7

mam komponentkątowa 2 wstawić @Component do DOM innego składnika

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'test-component', 
    template: '<b>Content</b>', 
}) 
export class TestPage { 
    constructor() {} 
} 

A mam inny komponent:

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'main-component', 
    templateUrl: 'main.html', 
}) 
export class MainPage { 

    constructor() {} 

    putInMyHtml() { 

    } 
} 

main.html:

<p>stuff</p> 
<div> <!-- INSERT HERE --> </div> 

Jak mogę dynamicznie wstawiać mój komponent TestPage do obszaru, w którym <!--INSERT HERE--> jest programowo, podobnie jak pl Prowadzę putInMyHtml.

Próbowałem edytować DOM i wstawić <test-component></test-component>, ale nie wyświetla tekstu zawartości z szablonu TestPage.

+0

Możliwy duplikat [Nie można zainicjować dynamicznie dołączane (HTML) składnik Kątowymi 2] (http://stackoverflow.com/questions/36566698/cant- initialize-dynamicznie-dołączony-html-component-in-angle-2) – echonax

+0

To jest poprawne rozwiązanie. Czy pojawił się komunikat o błędzie w konsoli przeglądarki lub kompilatorze? – Martin

+0

@Martin nie ma błędu, pozostaje on w DOM bez kątowego renderowania szablonu. – Akshat

Odpowiedz

11

Oto Plunker Example z ComponentFactoryResolver

Po pierwsze trzeba zarejestrować swoją dynamiczny składnik TestPage prawidłowo

app.module.ts

@NgModule({ 
    declarations: [MainPage, TestPage], 
    entryComponents: [TestPage] 
}) 

opcja alternatywna

Declare dynamicznie module.ts

import { NgModule, ANALYZE_FOR_ENTRY_COMPONENTS } from '@angular/core'; 

@NgModule({}) 
export class DynamicModule { 
    static withComponents(components: any[]) { 
    return { 
     ngModule: DynamicModule, 
     providers: [ 
     { 
      provide: ANALYZE_FOR_ENTRY_COMPONENTS, 
      useValue: components, 
      multi: true 
     } 
     ] 
    } 
    } 
} 

i zaimportować go w app.module.ts

@NgModule({ 
    imports:  [ BrowserModule, DynamicModule.withComponents([TestPage]) ], 
    declarations: [ MainComponent, TestPage ] 
}) 

Następnie komponent MainPage może wyglądać następująco:

import { ViewChild, ViewContainerRef, ComponentFactoryResolver } from '@angular/core'; 
@Component({ 
    selector: 'main-component', 
    template: ` 
    <button (click)="putInMyHtml()">Insert component</button> 
    <p>stuff</p> 
    <div> 
     <template #target></template> 
    </div> 
    ` 
}) 
export class MainPage { 
    @ViewChild('target', { read: ViewContainerRef }) target: ViewContainerRef; 
    constructor(private cfr: ComponentFactoryResolver) {} 

    putInMyHtml() { 
    this.target.clear(); 
    let compFactory = this.cfr.resolveComponentFactory(TestPage); 

    this.target.createComponent(compFactory); 
    } 
} 
+1

Fantastycznie, próbowałem wielu rzeczy, to jest ból głowy, Angular ciągle się zmienia – Akshat

1

Jeśli oba elementy są w tym samym module, upewnij się, że byli zarówno zajmą pierwsze:

mymodule

@NgModule({ 
    declarations: [TestPage, MainComponent] 
}) 

Jeśli są one w różnych modułów, upewnij się, że wywożone TestPage i zaimportował go do modułu, w którym ładujesz MainComponent:

TestPageModule

@NgModule({ 
    declarations: [TestPage], 
    exports: [TestPage] 
}) 

MainComponent

@NgModule({ 
    declarations: [MainComponent], 
    imports: [TestPage] 
}) 
Powiązane problemy