我写了以下指令,因为显然ng2-translate缺少一个:
import { Directive, ElementRef, Renderer, OnDestroy } from '@angular/core';
import { TranslateService } from 'ng2-translate';
import { Subscription } from 'rxjs';
@Directive({selector: '[translate]'})
export class TranslateDirective implements OnDestroy {
subscription: Subscription;
constructor(el: ElementRef, renderer: Renderer, translateService: TranslateService) {
let translateKey = el.nativeElement.attributes.translate.value;
this.subscription = translateService.get(translateKey).subscribe(value => {
el.nativeElement.innerHTML = value; // this works
// renderer.setText(el.nativeElement, value); // this doesn't work for some reason?
});
}
ngOnDestroy(): void {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}
正如你所看到的,我使用低级别的“el.nativeElement.innerHTML”让它工作,但 API 似乎暗示“renderer.setText”调用也应该工作。相反,它根本没有效果。
问:为什么会这样?setText 调用应该做什么?奖励:代码有问题还是 ng2-translate 不包含问题有充分的理由?馊主意?