使用* ngIf重新渲染组件

时间:2017-12-13 09:12:44

标签: angular

是否可以使用* ngIf在任何条件下重新渲染组件?我认为我可以进行超时,大约200ms,然后将组件值赋值为null,更新组件值并重新发送。 但是可以在没有超时的情况下重新渲染并更改组件的值吗?

示例:

@Component({})

export class RepComponent implements OnInit {
  component: any;
  
  constructor() {}


  ngOnInit() {
    this.component = this.route.snapshot.data['component'];
  }  
}
<div *ngIf="component.type === 'Text'">
<!-- ... -->
</div>

<div *ngIf="component.type === 'Image'">
<!-- ... -->
</div>

因此,由于示例,我需要如果component.type是“图像”,则组件本身会使用新数据进行重新渲染。

1 个答案:

答案 0 :(得分:0)

* ngIf已经在做你想要的了。当条件未满足时,它会从DOM中销毁组件的html部分。

也许你应该在ngOnInit的html中使用相同的变量?

&#13;
&#13;
ngOnInit() {
    this.component = this.route.snapshot.data[this.component.type];
  } 
&#13;
&#13;
&#13;

希望这有帮助

相关问题