How to re-trigger all pure pipes on all component tree in Angular 2

前端 未结 6 2244
青春惊慌失措
青春惊慌失措 2021-02-12 09:29

I have pure pipe TranslatePipe that translates phrases using LocaleService that has locale$: Observable current locale. I al

6条回答
  •  眼角桃花
    2021-02-12 10:06

    Pure pipes are only triggered when the input value changes.

    You could add an artificial additional parameter value that you modify

    @Pipe({name: 'translate'})
    export class TranslatePipe {
      transform(value:any, trigger:number) {
        ...
      }
    }
    

    and then use it like

    {{label | translate:dummyCounter}}

    Whenever dummyCounter is updated, the pipe is executed.

    You can also pass the locale as additional parameter instead of the counter. I don't think using |async for a single pipe parameter will work, therefore this might a bit cumbersome (would need to be assigned to a field to be usable as pipe parameter)

提交回复
热议问题