Javascript 可观察不';在ngIf中使用时,无法执行

Javascript 可观察不';在ngIf中使用时,无法执行,javascript,angular,rxjs,observable,Javascript,Angular,Rxjs,Observable,我试图根据模板中if条件中使用的布尔可观测值来执行可观测链/管道 <div>{{isAllowed$ | async}} --- resolves to true or false and shows on html page <div *ngIf="isAllowed$ | async> //other items </div> ngOnInit() { this.isAllowed$ = callToMethodThatRetu

我试图根据模板中if条件中使用的布尔可观测值来执行可观测链/管道

<div>{{isAllowed$ | async}} --- resolves to true or false and shows on html page
<div *ngIf="isAllowed$ | async>
    //other items
</div>

ngOnInit() {
    this.isAllowed$ = callToMethodThatReturnsBooleanObservalble();
  
    this.isAllowed$.pipe(
    tap(val => { // -- never gets executed
        if(val) {
            //make rest call --- never gets executed
        }
    })
)
}
但是pipe()语句中的代码永远不会执行,即使我在模板中可观察的布尔值上使用异步管道

<div>{{isAllowed$ | async}} --- resolves to true or false and shows on html page
<div *ngIf="isAllowed$ | async>
    //other items
</div>

ngOnInit() {
    this.isAllowed$ = callToMethodThatReturnsBooleanObservalble();
  
    this.isAllowed$.pipe(
    tap(val => { // -- never gets executed
        if(val) {
            //make rest call --- never gets executed
        }
    })
)
}
{{isAllowed$|async}--解析为true或false并显示在html页面上

您需要将管道添加到您进行的呼叫:

ngOnInit() {
  this.isAllowed$ = callToMethodThatReturnsBooleanObservalble().pipe(
    tap(val => { 
      if(val) {
           
      }
    })
  );
}

pipe
方法创建了一个新的
可观察对象
,因此,当您像以前那样进行操作时,该可观察对象不会被存储,也不会在模板中被订阅

您需要将管道添加到您所做的调用中:

ngOnInit() {
  this.isAllowed$ = callToMethodThatReturnsBooleanObservalble().pipe(
    tap(val => { 
      if(val) {
           
      }
    })
  );
}

pipe
方法创建了一个新的
可观察对象
,因此当您像以前一样进行操作时,该可观察对象不会被存储,也不会在模板中订阅

您可以使用Rxjs主题。这也可以作为一个可观察的,并且可以在您想要更新数据更改时使用“下一步”发出数据。无需手动订阅,因为您使用的是“异步”管道

您可以在代码中使用类似的内容

    <div>
      <div *ngIf="(isAllowed$ | async)">
        //other items
      </div>
    </div>;
   
   // In your .ts file //
   public isAllowed$ = new Subject<boolean>();

   ngOnInit() {
      // Pass your function that returns a boolean in your subject isAllowed.
      //  and your subscription will trigger whenever there is a change
      this.isAllowed$.next(callToMethodThatReturnsBooleanObservalble());
   }

//其他项目
;
//在您的.ts文件中//
允许公开$=新主题();
恩戈尼尼特(){
//传递在主题中返回布尔值的函数是允许的。
//只要有更改,您的订阅就会触发
此.isAllowed$.next(callToMethodThatReturnsBooleanObservable());
}

您可以使用Rxjs主题。这也可以作为一个可观察的,并且可以在您想要更新数据更改时使用“下一步”发出数据。无需手动订阅,因为您使用的是“异步”管道

您可以在代码中使用类似的内容

    <div>
      <div *ngIf="(isAllowed$ | async)">
        //other items
      </div>
    </div>;
   
   // In your .ts file //
   public isAllowed$ = new Subject<boolean>();

   ngOnInit() {
      // Pass your function that returns a boolean in your subject isAllowed.
      //  and your subscription will trigger whenever there is a change
      this.isAllowed$.next(callToMethodThatReturnsBooleanObservalble());
   }

//其他项目
;
//在您的.ts文件中//
允许公开$=新主题();
恩戈尼尼特(){
//传递在主题中返回布尔值的函数是允许的。
//只要有更改,您的订阅就会触发
此.isAllowed$.next(callToMethodThatReturnsBooleanObservable());
}

从模板中的订阅触发网络调用以呈现数据听起来不是个好主意。但是,从模板中的订阅触发网络调用以呈现数据听起来不是个好主意。