商店更新时重新呈现HTML-NGXS

时间:2019-04-24 15:03:30

标签: angular ngxs

我正在使用ngxs。存储状态正在更新,并且我正在根据需要获取值,但是我还想在存储值更新时再次重新呈现HTML。

现在我的代码说

this.store$ = this.store.select(state => state.departments.departments);

部门更新后,我需要我的组件重新呈现HTML。我做到了

<ng-container *ngIf="state$ | async">
    <router-outlet></router-outlet>
</ng-container>

值已得到体现,但HTML无法重新呈现。 我也尝试过

cdr.detectChanges(); or cdr.markForCheck()

但没有运气 还是商店无法满足我的需求?

1 个答案:

答案 0 :(得分:1)

尝试一下:

this.store$.subscribe(cdr.markForCheck);

这将确保只要store $可观察到的更改,Angular就会在您的组件上运行其更改检测并相应地更新视图。

另一种方法是将store $表示为@Input()属性。因为Angular会连续监视组件的所有输入属性,即使将changeDetectionStrategy设置为“ onPush”也是如此。

相关问题