2
我有一个管道解析消息密钥并相应地返回消息到语言环境。Angular 2让它等待服务初始化
但我想角等待直到我的服务将完成它的加载消息,并且只有在那之后继续呈现主页面。我的本地化管道在哪里使用。 如何让它工作?
服务:
@Injectable()
export class I18nService implements OnInit {
private cachedMessages: {string: string};
activeLocale:I18Enum = null;
constructor(private http: Http) {
this.reloadLocale(I18Enum.ru);
}
ngOnInit(): void {
this.reloadLocale(I18Enum.ru);
}
getMessage(key: string) {
if (this.cachedMessages) {
return this.cachedMessages[key];
}
}
reloadLocale(locale: I18Enum) {
this.activeLocale = locale;
this.http.get(basePath + i18nPath + I18Enum[locale]).subscribe(
res => {
this.cachedMessages = res.json();
}
);
}
}
和管道:
@Pipe({name: 'i18nPipe'})
export class I18nPipe implements PipeTransform {
constructor(private i18Service: I18nService) {
}
transform(value: any, args: any): any {
return this.i18Service.getMessage(value);
}
}
可能复制[如何将从后端呈现的参数传递给angular2引导方法](http://stackoverflow.com/questions/37611549/how-to-pass-parameters-rendered -from-后端到angular2的自举方法) – estus