Answer the question
In order to leave comments, you need to log in
How to make pipe work asynchronously without always specifying |async in templates in angular 2?
@Pipe({
name: 'someRand',
pure: false
})
export class RandomPipe implements PipeTransform {
constructor(private cdRef:ChangeDetectorRef) {
}
transform(value:string):any {
return new AsyncPipe(this.cdRef).transform(new Observable<string>(observer=>{
observer.next('rand1');
setTimeout(()=>{
observer.next('[email protected]');
})
}));
}
}
Answer the question
In order to leave comments, you need to log in
I think it's possible this way
@Pipe({
name: 'someRand',
pure: false
})
export class RandomPipe implements PipeTransform {
constructor(private cdRef:ChangeDetectorRef) {}
pipe: AsyncPipe;
obs: Observable<string>;
transform(value:string):any {
if (!this.pipe) {
this.pipe = new AsyncPipe(this.cdRef);
this.obs = new Observable<string>(observer=>{
observer.next('rand1');
setTimeout(()=>{
observer.next('[email protected]');
}, 600)
});
}
return this.pipe.transform(this.obs);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question