Angular material displayWith 不适用于 ngx-translate

Angular material displayWith isn't working with ngx-translate

我在 mat-autocomplete 中使用 [displayWith] 指令。当我手动选择值时它工作正常但是当我重新加载页面时我没有得到翻译。翻译所需的参数是从 ngOnInit 中的查询参数异步加载的。所以我依赖异步参数,但我的 displayFunction() 是同步函数。如何解决?

没有 [displayWith] 功能,一切正常,但没有翻译(它只是显示我不想要的纯值)。所以我确信其余代码是正确的。

我的垫子自动完成:

<mat-form-field [formGroup]="cityForm"
                appearance="outline"
                floatLabel="never"
                color="primary">
  <mat-icon matPrefix>location_on</mat-icon>
  <input type="text" placeholder="{{ 'job_offer_search_bar.job-offer-search-bar-city-form.placeholder' | translate }}"
         aria-label="Number" matInput
         formControlName="cityControl" [matAutocomplete]="auto">
  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="onSelectionChanged($event.option.value)"
                    [displayWith]="displayFn.bind(this)">
    <mat-option>
      {{ 'job_offer_search_bar.job-offer-search-bar-city-form.all' | translate }}
    </mat-option>
    <mat-option *ngFor="let city of filtredCities | async" [value]="city">
      {{ 'job_offer_search_bar.job-offer-search-bar-city-form.city' | translate:"{ city: '" + city +"' }"}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

我的 displayWith 函数如下:

displayFn(val: string){
    if (!val) return '';
    let stringToReturn;
    this.translate.get('job_offer_search_bar.job-offer-search-bar-city-form.city', {city: val}).subscribe(value => {
      console.log('inside subscribe', value);
      stringToReturn = value;
    });
    console.log('after sub', stringToReturn);
    if (stringToReturn != undefined) {
      return stringToReturn;
    } else {
      return 'Sorry, value has not been translated';
    }

Console.log in subscribeconsole.log after subscribe 之后调用。所以订阅是在我得到我的翻译参数之后进行的,所以在我的 return... 我需要一些技巧或技巧来将翻译后的字符串作为 return.

传递

我认为有办法做到这一点。任何帮助将不胜感激。

在大多数情况下,Observables 是异步函数。 根据您的实施,您可以使用 translate.instant

displayFn(val: string) {

  const defaultMessage = 'Sorry, value has not been translated';
  return val
    ? this.translate.instant('job_offer_search_bar.job-offer-search-bar-city-form.city', { city: val }) || defaultMessage
    : '';

}

如果在加载翻译文件之前调用即时函数,它将return未定义。

编辑:

displayFn(val: string) {

  const translate$ = this.translate.get('job_offer_search_bar.job-offer-search-bar-city-form.city', { city: val }).pipe(
    map(translatedText => translatedText || 'Sorry, value has not been translated')
  )

  return val
    ? translate$
    : of('');

}


[displayWith]="displayFn.bind(this) | async"

问题的解决很棘手。我不得不在 mat-form-field 上使用 *ngIf 来等待我翻译的标签:

<mat-form-field *ngIf="isReady$ | async" [formGroup]="cityForm"

翻译完成后应满足条件,因此我不得不在 ngOnInit 中执行此操作:

 this.isReady$ = this.translate.get('translate_id').pipe(mapTo(true));

所以现在在翻译服务返回翻译之前不会显示元素。这是一种解决方法,但我还没有找到其他解决方案。

如果您正在使用 changeDetection: ChangeDetectionStrategy.OnPush 策略

您可以将 AfterViewChecked 工具添加到您的组件中,并且

ngAfterViewChecked 钩子上,添加下一个代码:

ngAfterViewChecked() {
   this._translateService.onLangChange.subscribe(() => {
       this._changeDetectorRef.detectChanges()
   });
}

在这种方法中,您无需在模板中做任何事情。

它对我有用。享受吧。