异步管道与主题组合不起作用ngIf

时间:2018-07-19 22:45:36

标签: angular asynchronous pipe subject ngif

我在服务中有一个BehaviorSubject,它期望一个LoadingParams对象,该对象上具有一个isLoading布尔变量:

  showLoadingSubject = new BehaviourSubject<LoadingParams>(
                         new LoadingParams(false, 'some text')
                       );

我正在组件中按如下方式使用它:

 showLoading: this.theService.showLoadingSubject.asObservable();

在模板中是

<div class="loading-screen" *ngIf="showLoading.isLoading | async" #containerDivSuper> 
some content in the div 
    </div>

然后div应该显示为

this.showloadingSubject.next(new LoadingParams(true, 'somestring'));

但是下一次通话后div不会显示。

有人知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

好吧,它与指令Async pipe not working with Subject一样,都是用指令的语法解决的。

<div class="loading-screen" 
      *ngIf="showLoading ? (showLoading | async).isLoading : false" #containerDiv>
   Some text
</div>

我还添加了一项检查,以查看可观察对象是否未定义,因为我收到了未定义的错误消息。