角2字典 [英] Angular 2 dictionary
本文介绍了角2字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var rus = {hello:我的代码看起来像这样:我有2个字典和函数привет,
text:текст,
home:дом};
var eng = {hello:hello,
text:text,
home:home};
$ scope.selectedLang = rus;
translate();
function translate(){
$ scope.hello = $ scope.selectedLang [hello];
$ scope.text = $ scope.selectedLang [text];
$ scope.home = $ scope.selectedLang [home];
}
$ scope.switchLang = function(lang){
if(lang ==rus){
$ scope.selectedLang = rus;
} else {
$ scope.selectedLang = eng;
}
translate();
};
但是现在我需要把它放在角度2.我该怎么做?
解决方案
我只包括课堂逻辑,希望这有助于:
导出类AppComponent实现OnInit {
public hello:any;
public text:any;
public home:any;
private rus = {
hello:привет,
text:текст,
home:дом
};
private eng = {
hello:hello,
text:text,
home:home
};
private selectedLang:any;
ngOnInit(){
this.selectedLang = this.rus;
this.switchLang('rus'); //调用switchLang()方法
}
private selectedLang = this.rus;
translate(){
this.hello = this.selectedLang [hello];
this.text = this.selectedLang [text];
this.home = this.selectedLang [home];
}
switchLang(lang:string){
if(lang ==rus){
this.selectedLang = this.rus;
} else {
this.selectedLang = this.eng;
}
this.translate();
}
}
In Angular 1 my code looked like this:I have 2 dictionaries and functions
var rus = {"hello" : "привет",
"text":"текст",
"home":"дом"};
var eng = {"hello":"hello",
"text":"text",
"home":"home"};
$scope.selectedLang = rus;
translate();
function translate() {
$scope.hello = $scope.selectedLang["hello"];
$scope.text = $scope.selectedLang["text"];
$scope.home = $scope.selectedLang["home"];
}
$scope.switchLang = function(lang) {
if(lang == "rus") {
$scope.selectedLang = rus;
} else {
$scope.selectedLang = eng;
}
translate();
};
But now I need to make this in angular 2. How can I do this?
解决方案
I have included only class logic here,hope this helps:
export class AppComponent implements OnInit{
public hello:any;
public text:any;
public home:any;
private rus = {
"hello" : "привет",
"text":"текст",
"home":"дом"
};
private eng = {
"hello":"hello",
"text":"text",
"home":"home"
};
private selectedLang:any;
ngOnInit(){
this.selectedLang = this.rus;
this.switchLang('rus'); //calling switchLang() method
}
private selectedLang = this.rus;
translate() {
this.hello = this.selectedLang["hello"];
this.text = this.selectedLang["text"];
this.home = this.selectedLang["home"];
}
switchLang (lang:string){
if(lang == "rus") {
this.selectedLang = this.rus;
} else {
this.selectedLang = this.eng;
}
this.translate();
}
}
这篇关于角2字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文