Angular 角度侦听返回某个变量的函数
因此,我的服务中有一个函数返回一个值,我希望我的组件监听该值,并在它发生变化时更新它,所以我认为订阅它是一个好主意,但它不能按预期工作,并且在它发生变化时不更新该值。有没有其他方法可以做到这一点Angular 角度侦听返回某个变量的函数,angular,service,components,Angular,Service,Components,因此,我的服务中有一个函数返回一个值,我希望我的组件监听该值,并在它发生变化时更新它,所以我认为订阅它是一个好主意,但它不能按预期工作,并且在它发生变化时不更新该值。有没有其他方法可以做到这一点 ngOnInit(): void { this.translateService.onLangChange.subscribe(() => this.getLocaleDateFormat()); } getLocaleDateFormat(): Observable<st
ngOnInit(): void {
this.translateService.onLangChange.subscribe(() => this.getLocaleDateFormat());
}
getLocaleDateFormat(): Observable<string> {
this.dateAdapter.setLocale(Languages[this.translateService.currentLang]);
this.currLang = Languages[this.translateService.currentLang];
console.log( 'get' + this.currLang);
return of (this.currLang);
}
ngOnInit(): void {
this.localeDateAdapterService.getLocaleDateFormat().subscribe(currLang => this.currLang = currLang);
}
使用/rxjs中的主题
// Service
language$ = new Subject<string>();
setLanguage(lang: string): void {
this.language$.next(lang);
}
// Component
this.language$.subscribe((res: string) => {
console.log(res);
});
this.languageSrv.setLanguage('en');
this.languageSrv.setLanguage('es');
this.languageSrv.setLanguage('it');
this.languageSrv.setLanguage('fr');
// 'en'
// 'es'
// 'it'
// 'fr'
//服务
语言$=新主题();
setLanguage(lang:string):无效{
this.language$.next(lang);
}
//组成部分
this.language$.subscribe((res:string)=>{
控制台日志(res);
});
this.languageSrv.setLanguage('en');
this.languageSrv.setLanguage('es');
this.languageSrv.setLanguage('it');
this.languageSrv.setLanguage('fr');
//“嗯”
//“是的”
//“是的。”
//“fr”
始终记住取消订阅我将其作为一项单独的服务,名为LocaleService。您必须以某种方式从translate服务获取当前语言,或者将其作为单独的服务来处理
private onLanguageChange(lang: string): void {
// To add another lang:
// 1. add mapping from EMS language codes to Angular locale codes in the map at the bottom of this file
// 2. register locale data, also at the bottom of this file
const locale = Languages[lang];
this.activeLocale.next(locale);
this.dateAdapter.setLocale(locale);
this.LW.log('Language change detected (current lang: ' + lang + '), setting locale to: ' + locale);
}
public getActiveLocale(): BehaviorSubject<string> {
return this.activeLocale;
}
export const Languages = {
'eng': 'en',
'slk': 'sk',
'cze': 'cs',
};
registerLocaleData(localeSk);
registerLocaleData(localeCs);
这不会因为组件需要服务而服务需要组件而创建循环依赖关系吗?
private onLanguageChange(lang: string): void {
// To add another lang:
// 1. add mapping from EMS language codes to Angular locale codes in the map at the bottom of this file
// 2. register locale data, also at the bottom of this file
const locale = Languages[lang];
this.activeLocale.next(locale);
this.dateAdapter.setLocale(locale);
this.LW.log('Language change detected (current lang: ' + lang + '), setting locale to: ' + locale);
}
public getActiveLocale(): BehaviorSubject<string> {
return this.activeLocale;
}
export const Languages = {
'eng': 'en',
'slk': 'sk',
'cze': 'cs',
};
registerLocaleData(localeSk);
registerLocaleData(localeCs);
this.localeService.getActiveLocale().subscribe(locale => this.currentLocale = locale));