2016-11-12 30 views
7

Tôi có một thành phầngóc 2 chèn một @Component vào DOM của một thành phần

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

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

Và tôi có một thành phần:

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> 

Làm cách nào tôi có thể chèn động thành phần TestPage của mình vào khu vực nơi <!--INSERT HERE--> có lập trình, như vi Tôi chạy putInMyHtml.

Tôi đã thử chỉnh sửa DOM và chèn <test-component></test-component> nhưng không hiển thị văn bản nội dung từ mẫu của TestPage.

+0

có thể trùng lặp của [Có thể không khởi tạo tự động nối (HTML) thành phần trong góc 2] (http://stackoverflow.com/questions/36566698/cant- initialize-dynamically-appended-html-component-in-angular-2) – echonax

+0

Đó là giải pháp đúng. Bạn có nhận được thông báo lỗi trong bảng điều khiển trình duyệt hoặc trình biên dịch của bạn không? – Martin

+0

@Martin không có lỗi, nó vẫn là trong DOM mà không cần hiển thị mẫu. – Akshat

Trả lời

11

Dưới đây là một Plunker Example với ComponentFactoryResolver

Trước hết bạn phải đăng ký thành phần động của bạn TestPage đúng

app.module.ts

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

lựa chọn Alternative

Khai động-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 
     } 
     ] 
    } 
    } 
} 

và nhập nó trong app.module.ts

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

Sau đó, thành phần MainPage của bạn có thể trông như sau:

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

Tuyệt vời, tôi đã thử rất nhiều thứ, đây là một nhức đầu, Angular liên tục thay đổi – Akshat

1

Nếu cả hai thành phần là trong các mô-đun cùng, chắc chắn rằng họ đều đã tuyên bố đầu tiên:

MyModule

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

Nếu họ đang có trong module khác nhau, chắc chắn rằng bạn đã xuất khẩu TestPage và đã nhập nó vào mô-đun nơi bạn tải MainComponent:

TestPageModule

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

MainComponent

@NgModule({ 
    declarations: [MainComponent], 
    imports: [TestPage] 
}) 
Các vấn đề liên quan