编程语言
首页 > 编程语言> > javascript-Angular 2 TranslateService检测语言更改

javascript-Angular 2 TranslateService检测语言更改

作者:互联网

如何检测其他组件中的语言变化?我的检测是在标头组件中进行的.我的目标是检测语言变化并设置样式.

子组件:

import { TranslateService } from "ng2-translate";
export class ChildComponent{
    public browserLang: string;
    constructor(private translate: TranslateService) {
        this.browserLang = translate.getBrowserLang();
    }
}

儿童的HTML:

<div ng-class="{black: browserLang == 'en', red: browserLang == 'de'}" >

结果是:未添加类.如果我输出语言字符串(en)是正确的.但是它没有听任何更改.如何使其听和改变课堂?

解决方法:

添加这些就可以了:

import {TranslateService, LangChangeEvent} from "ng2-translate"

export class ChildComponent {
    public browserLang: string;
    constructor(private translate: TranslateService) {
        translate.onLangChange.subscribe(lang=>{
            this.browserLang = lang;
        })
    }
    ngOnInit(){
        this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
            this.browserLang = event.lang
        });
    }
}

标签:angular-translate,angular,html,javascript
来源: https://codeday.me/bug/20191111/2021408.html