首页 文章

使用* ngIf重新渲染组件

提问于
浏览
0

是否可以使用* 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 是'Image',组件本身会重新渲染新数据 .

1 回答

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

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

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

    希望这有帮助

相关问题