I have the following code that is supposed to display the length of an audio file:
ngAfterViewInit() {
this.duration$ = fromEvent(this.player.nativeElement, 'loadeddata').pipe(
map((event: any) => {
return this.convertDuration(event.target.duration);
}));
}
But it won't display on my HTML that looks like this:
<div>{{ duration$ | async }}</div>
What could be the problem here? I tried to subscribe in my component and this works:
this.duration$.subscribe(x => console.log(x));
Initialization: duration$: Observable<string>;
After looking at @EliyaCohen's demo, I realized that the reason why my code doesn't work is because I have ChangeDetectionStrategy.OnPush enabled. I think my issue now is how to trigger a change detection without calling detectChanges() or markForChange() if that's possible.
Here is my demo.
return this.convertDuration(event.target.duration);withreturn 10;?subscribe()function works as expected so it's weird why it doesn't print in the HTML.this.duration$.subscribe(x => console.log(x));is used right after the assignment in ngAfterViewInit?convertDuration()returns the mm:ss format of the duration.this.duration$.subscribe(x => console.log(x));is below the assignment and I only added that line to test that the observable works. It's not really part of the code.<div *ngIf="duration$">{{ duration$ | async }} </div>