Skip to content

Instantly share code, notes, and snippets.

@FFKL
Created January 9, 2020 12:49
Show Gist options
  • Save FFKL/cfd3c6b551f4c04f2bdff6f11a1e42ed to your computer and use it in GitHub Desktop.
Save FFKL/cfd3c6b551f4c04f2bdff6f11a1e42ed to your computer and use it in GitHub Desktop.
import { ChangeDetectorRef, OnDestroy, Pipe, PipeTransform } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { get } from 'lodash';
import { Subscription } from 'rxjs';
import { ToursCatalog } from 'package-types';
@Pipe({
name: 'departureCityLabel',
pure: false
})
export class DepartureCityLabelPipe implements PipeTransform, OnDestroy {
private onLangChange: Subscription;
constructor(private translateService: TranslateService, private cd: ChangeDetectorRef) {}
transform(value: ToursCatalog.DepartureSelector): string {
if (!this.onLangChange) {
this.onLangChange = this.translateService.onLangChange.subscribe(() => this.cd.markForCheck());
}
return get(value, `text.${this.translateService.currentLang}`);
}
ngOnDestroy(): void {
if (this.onLangChange) {
this.onLangChange.unsubscribe();
this.onLangChange = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment