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
}
'DynamicComponentLoader' jest teraz przestarzałe :( –
@ 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 ;-) –