Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 当语言更改时,执行新的服务调用_Angular_Typescript - Fatal编程技术网

Angular 当语言更改时,执行新的服务调用

Angular 当语言更改时,执行新的服务调用,angular,typescript,Angular,Typescript,我有一个路由到的组件,因此它没有父/子组件。我通过TranslateService更改语言,该服务从我的app.component中调用,它是我的应用程序的基础。我调用一个服务从后端获取一个HTML文件,但是当我更改语言时,我想执行一个新的调用以获取另一种语言的文档 我不能使用ngOnchanges,因为没有@Input() app.component.ts @Component({ selector: 'app-root', templateUrl: './app.compon

我有一个路由到的组件,因此它没有父/子组件。我通过
TranslateService
更改语言,该服务从我的
app.component
中调用,它是我的应用程序的基础。我调用一个服务从后端获取一个HTML文件,但是当我更改语言时,我想执行一个新的调用以获取另一种语言的文档

我不能使用
ngOnchanges
,因为没有
@Input()

app.component.ts

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['app.component.scss']
})
export class AppComponent {
    constructor(private translateService: TranslateService) {
    }

    public changeLanguage(language) {
        this.translateService.use(language);
        this.translateService.setDefaultLang(language);
    }
}
免责声明

@Component({
    selector: 'app-disclaimer',
    templateUrl: 'vastgeklikteRechten.component.html',
    styleUrls: ['vastgeklikteRechten.component.scss']
})

export class VastgeklikteRechtenComponent {
    @ViewChild('dataContainer') dataContainer: ElementRef;

    text = '';

    constructor(
        private translateService: TranslateService,
        private vastgeklikteRechtenService: VastgeklikteRechtenService
    ) {
        this.vastgeklikteRechtenService.getVastgeklikteRechten(
        this.getLanguage()).subscribe(text => {
            this.text = text;
            this.loadData(text);
        });
    }

    loadData(data) {
        this.dataContainer.nativeElement.innerHTML = data;
    }

    getLanguage() {
        return this.translateService.getDefaultLang();
    }
}

在我的免责声明中更改了我的构造函数。我仍然需要原始调用从我的服务中获取html文件,以下是构造函数:

constructor(private location: Location,
          private translateService: TranslateService,
          private vastgeklikteRechtenService: VastgeklikteRechtenService) {
this.vastgeklikteRechtenService.getVastgeklikteRechten(this.getLanguage()).subscribe(text => {
  this.text = text;
  this.loadData(text);
});
this.onLanguageChanged();
}
我还添加了一个名为onLanguageChanged()的方法,在其中订阅在TranslationService中找到的eventEmitter。所以现在当“onLangChange”事件发生时,我会打一个新的电话

onLanguageChanged() {
this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
  this.vastgeklikteRechtenService.getVastgeklikteRechten(
    event.lang).subscribe(text => {
    this.text = text;
    this.loadData(text);
  });
});
}