, , , - ComponentRef dispose() , . .
export class App {
...
private _children:ComponentRef[] = [];
add() {
this._dcl.loadIntoLocation(DynamicCmp, this._e, 'location').then((ref) => {
...
this._children.push(ref);
});
}
removeall(){
this._children.forEach(cmp=>cmp.dispose());
this._children = []; // not even necessary to get the components off the screen
}
}
- , dispose() , "", DynamicComponent , , .
, , , , , ...
: , , , , DynamicComponentLoader, .
, , DCL, ( ) , , , , .
- . :
@Component({
selector: 'my-app',
template : `
<button (click)="add()">Add new component</button>
<button (click)="removeAll()">Remove All</button>
<template ngFor #child [ngForOf]="children">
<div [dynamicCmp]="child"></div>
</template>
`,
directives:[DynamicCmp]
})
export class App {
children:number[] = [];
add() {
this.children.push(this.children.length+1);
}
removeAll(){
this.children = [];
}
}