2016-01-18 10 views
12

Planuję dodać dynamiczną komponent do DOM, jeśli wywoływana jest show(). Wiem, że istnieje rozwiązanie z ngIf lub [hidden] do ukrycia go i użycia go jako dyrektywy, ale nie jestem fanem tego rozwiązania, ponieważ nie chcę zadeklarować go w moim kodzie HTML.Angular 2 append Dynamiczny komponent do DOM lub szablonu

import {Component} from 'angular2/core'; 
    import {InfoData} from '../../model/InfoData'; 

    @Component({ 
    selector: 'Info', 
    templateUrl: './components/pipes&parts/info.html', 
    styleUrls: ['./components/pipes&parts/info.css'] 
    }) 

    export class Info{ 
    infoData: InfoData; 

    public show(infoData: InfoData) { 
     this.infoData= infoData; 
     document.body.appendChild(elemDiv); <----- Here? 
    } 
    } 

a następnie deklaruję to jako dostawcę, więc mogę wywołać show().

import {Component} from 'angular2/core'; 
    import {Info} from './components/pipes&parts/Info'; 

    @Component({ 
    selector: 'Admin', 
    templateUrl: './Admin.html', 
    styleUrls: ['./Admin.css'], 
    directives: [Info], 
    providers: [Info] 
    }) 

    export class Admin { 
    constructor(private info: Info) { 
    info.show(); <---- append the Info Element to DOM 
    } 

Odpowiedz

6

Nie sądzę, że musisz dostarczyć komponent Info jako dostawcę do drugiego komponentu. Nie jestem pewien, czy to działa. Można wykorzystać Query i QueryView odwołać komponenty używane w innym:

@Component({ 
    selector: 'Admin', 
    templateUrl: './Admin.html', 
    styleUrls: ['./Admin.css'], 
    directives: [Info] 
}) 
export class Admin{ 
    constructor(private @Query(Info) info: QueryList<Info>) { 
    info.first().show(); <---- append the Info Element to DOM 
    } 
} 

Zamiast dodawania elementu w ramach komponentu Info można dynamicznie dodać ten składnik pomocą DynamicComponentLoader jak sugeruje Günter:

@Component({ 
    selector: 'Info', 
    templateUrl: './components/pipes&parts/info.html', 
    styleUrls: ['./components/pipes&parts/info.css'] 
}) 

export class Info{ 
     infoData: InfoData; 

    public show(infoData: InfoData) { 
    this.infoData= infoData; 
    // No need to add the element dynamically 
    // It's now part of the component template 
    // document.body.appendChild(elemDiv); <----- Here? 
    } 
} 

@Component({ 
    selector: 'Admin', 
    //templateUrl: './Admin.html', 
    // To show where the info element will be added 
    template: ` 
    <div #dynamicChild> 
     <!-- Info component will be added here --> 
    </div> 
    `, 
    styleUrls: ['./Admin.css'], 
    directives: [Info] 
}) 
export class Admin{ 
    constructor(private dcl: DynamicComponentLoader, private eltRef:ElementRef) { 
    this._dcl.loadIntoLocation(Info, this._el, 'dynamicChild') 
     .then(function(el) { 
      // Instance of the newly added component 
     }); 
    } 
} 

Mam nadzieję, że to pomoże, Thierry

+3

'DynamicComponentLoader' jest teraz przestarzałe :( –

+2

@ NoémiSalaün tak, ale ta odpowiedź Günter może Cię zainteresować: http: // stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-selected-components/36325468 # 36325468 ;-) –

7
+0

Czy możesz zajrzeć na to pytanie, https://stackoverflow.com/questions/47655224/load-ngfor-inside-in sertadjacenthtml. – klaudia

Powiązane problemy