Combine two or more (boolean) observables on single ngIf using async pipe

后端 未结 3 1712
谎友^
谎友^ 2021-02-13 06:02

Without observables I can write the following line in the HTML template:

3条回答
  •  感情败类
    2021-02-13 06:23

    What about using combineLatest?

    For example:

    import { combineLatest } from 'rxjs/observable/combineLatest';
    import { Observable } from 'rxjs/Observable';    
    
    @Component({...})
    export class FooComponent {
      obs1$: Observable;
      obs2$: Observable;
      obs3$: Observable;
    
      constructor(){
        // set observables
      }
    
      get combined$(){
        return combineLatest(
          this.obs1$,
          this.obs2$
          this.obs3$,
          (one,two,three)=>(one || two) && three);
      }
    }
    
    // template
    

    Check the following fiddle for guidance:

    https://jsfiddle.net/uehasmb6/11/

    More info about the combineLatest operator here

    UPDATE: But if you still want to keep all that logic inside of the template, you could try something like:

    But I would advice you against this. Keeping the HTML template as clean as possible is a good practice.

提交回复
热议问题