我只是看ng2Translate所以我可以在我的Ionic2 / Angular2
项目中使用它进行本地化。我已经设置并正常工作,并根据文档使用时在标记中获取正确的值...
<div>{{myVal}}</div>
<div>{{ 'COMMON.HELLO' | translate:{value: "world"} }}</div>
我的字符串值很好。
我现在想尝试在后面的代码中获取一些字符串,并将它们分配给组件变量,然后我可以在标记中绑定它们,如下所示。
@Component({
templateUrl: 'build/pages/sidemenu-page/sidemenu-page.html',
pipes: [TranslatePipe]
})
export class SideMenuPage {
public rootPage = SideMenuPage;
public myVal: string;
constructor(private menu: MenuController, private nav: NavController, private tranlate: TranslateService){
let temp = tranlate.get("MYTESTVAL");
this.myVal = temp.first(); // <-- type error first does not return a string
}
然后使用它......
<div>{{myVal}}</div>
tranlate.get
返回一个RxJs可观察对象(我刚才开始看,我之前没有使用它们) - 我只是看不到如何从中获取“原始字符串值”。事件first
似乎仍然会返回一个可观察的事件。
任何人都可以告诉我如何获取字符串吗?
答案 0 :(得分:0)
阅读RxJs,看起来我需要做的就是
temp.subscribe(x => this.myVal = x)
这无疑无论如何都有效。
[UPDATE]
事件更好,有一个即时方法.. this.myVal = tranlate.instant("MYTESTVAL");
答案 1 :(得分:0)
你试过这个:
@Component({
templateUrl: 'build/pages/sidemenu-page/sidemenu-page.html',
pipes: [TranslatePipe]
})
export class SideMenuPage {
public rootPage = SideMenuPage;
public myVal: string;
public sub: any;
constructor(private menu: MenuController, private nav: NavController, private tranlate: TranslateService){
this.sub = tranlate.get("MYTESTVAL").subscribe(value=>this.myVal=value,error=>console.log(error));
}
ngOnDestroy(){
this.sub.unsubscribe();
}
}