【问题标题】:Angular 5: Updating template in Observable chain not workingAngular 5:在 Observable 链中更新模板不起作用
【发布时间】:2018-06-05 23:37:15
【问题描述】:

如果我有一个组件并想显示一个加载器

组件

  ngOnInit() {
    this.route.params
      .do(x => this.contentLoading = true)
      .switchMap(x => this.getClientSearchResults())
      .subscribe(x => {
        this.clientSearchResults = x;
        this.contentLoading = false
      });
  }

模板

<mat-progress-spinner *ngIf="contentLoading" color="accent" mode="indeterminate"></mat-progress-spinner>

加载器将在页面第一次加载时显示,但是对参数的任何更改都将不再显示加载器。我可以看到 Observable 链每次都以console.log 运行。 ngZone 在运行 Observable 链时是否检测不到它?

【问题讨论】:

    标签: javascript angular angular5 observable


    【解决方案1】:

    switchMap完成之前的 observable,并切换到新的 observable。这意味着在发出第一个参数更改后,subscribe 将不再接收未来的更改。

    我想你想改用mergeMap

    https://www.learnrxjs.io/operators/transformation/mergemap.html

    【讨论】:

    • 即使我将其更改为mergeMap,尽管运行代码this.contentLoading = true,它仍然没有显示我的加载器第二次搜索
    猜你喜欢
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多