Angular 2 - Endless loop in async pipe

前端 未结 3 766
囚心锁ツ
囚心锁ツ 2021-01-04 22:31

I`m getting an endless loop when I try to bind a async function like this:


     {{myAsyncFunc(i) | async}}<         


        
3条回答
  •  误落风尘
    2021-01-04 23:03

    If your async/observable requires you to pass a parameter (e.g., you are inside an ngFor loop) perhaps you can create a custom async pipe for that.

    @Pipe({
      name: 'customPipe'
    })
    export class customPipe implements PipeTransform {
    
      constructor(private someService: SomeService) {}
    
      /**
       * 
       * @param id 
       */
      transform(id: number): Observable {
        return this.someService.shouldShow(id);
      }
    
    }
    

    And in your template you can call your async pipe as:

    {{id | customPipe | async}}
    

提交回复
热议问题