ComponentFactoryResolver与* ngIf

时间:2018-06-23 07:26:23

标签: angular

下面我创建了一个动态组件

AppComponent.html

<input type="button" value="One" (click)="setVal('one')">
<input type="button" value="Two" (click)="setVal('two')">
<br>
<app-dynamic-component [key]="keyvalue"></app-dynamic-component>

AppComponent.ts

keyvalue = 'one'

setVal(val) {
    this.keyvalue = val;
}

及其记录:

enter image description here


以下正在使用*ngIf

AppComponent.html

<input type="button" value="One" (click)="setVal('one')">
<input type="button" value="Two" (click)="setVal('two')">
<br>
<app-component-one *ngIf="keyvalue=='one'"></app-component-one>
<app-component-two *ngIf="keyvalue=='two'"></app-component-two>

AppComponent.ts

keyvalue = 'one'

setVal(val) {
    this.keyvalue = val;
}

和日志:

enter image description here


两者都具有创建和销毁子组件的相同操作。相对于ComponentFactoryResolver使用*ngIf有什么好处,可以说*ngIf完成了工作,我应该改为使用ComponentFactoryResolver吗?

0 个答案:

没有答案