Angularjs 如何在组件内部使用ngx翻译
我在我的网站中使用ngx translate,唯一的问题是我知道如何在html模板中使用它,它工作得很好,但我如何调用Angularjs 如何在组件内部使用ngx翻译,angularjs,ngx-translate,Angularjs,Ngx Translate,我在我的网站中使用ngx translate,唯一的问题是我知道如何在html模板中使用它,它工作得很好,但我如何调用JSON内部组件的键 这就是我所需要的我有 app.html <div>{{"home" | translate }}</div> 我如何替换这个主页以从json文件读取?我找到了一个解决方案。需要注意的一点是,我必须利用DoCheck lifecycle事件,以便组件级标签可以在不刷新的情况下进行转换 我是这样做的 import { Componen
JSON
内部组件的键
这就是我所需要的我有
app.html
<div>{{"home" | translate }}</div>
我如何替换这个主页以从
json
文件读取?我找到了一个解决方案。需要注意的一点是,我必须利用DoCheck lifecycle事件,以便组件级标签可以在不刷新的情况下进行转换
我是这样做的
import { Component, DoCheck } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-some',
templateUrl: './some.component.html',
styleUrls: ['./some.component.css']
})
export class SomeComponent implements DoCheck {
headers = [];
constructor(public translate: TranslateService){
}
ngDoCheck() {
this.translate.get(['HEADER.PRIORITY', 'HEADER.STATE', 'HEADER.DATE_CREATED'])
.subscribe(translations => {
this.setHeaders(translations);
});
}
setHeaders(translations) {
this.headers = [
{
title: translations['HEADER.PRIORITY'],
active: true
},{
title: translations['HEADER.STATE'],
active: false
},{
title: translations['HEADER.DATE_CREATED'],
active: false
}];
}
}
你知道吗?不,我一点也不知道:(
import { Component, DoCheck } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-some',
templateUrl: './some.component.html',
styleUrls: ['./some.component.css']
})
export class SomeComponent implements DoCheck {
headers = [];
constructor(public translate: TranslateService){
}
ngDoCheck() {
this.translate.get(['HEADER.PRIORITY', 'HEADER.STATE', 'HEADER.DATE_CREATED'])
.subscribe(translations => {
this.setHeaders(translations);
});
}
setHeaders(translations) {
this.headers = [
{
title: translations['HEADER.PRIORITY'],
active: true
},{
title: translations['HEADER.STATE'],
active: false
},{
title: translations['HEADER.DATE_CREATED'],
active: false
}];
}
}