When there are updates in language, a new service request is needed

I'm currently working on a component that I navigate to, which means it doesn't have a parent or child. The language in this component is changed using the TranslateService, and this service is called from my app.component, which acts as the base of the application. The issue I'm facing is that when I change the language, I need to make a new call to get the document in the updated language from the backend.

One constraint is that I cannot use ngOnChanges because there is no @Input(), and modifying the backend is not an option as it is part of a legacy application that is also used by another application.

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);
    }
}

Disclaimer

@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();
    }
}

Answer №1

Updated my constructor in the disclaimer component. I still require the original call to retrieve the HTML file from the service. Below is the revised constructor:

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();
}

In addition, I implemented a new method named onLanguageChanged() where I subscribe to the eventEmitter found in the TranslationService. This enables a new call to be made when the 'onLangChange' event is triggered.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to exclude React.js source files from a fresh Nest.js setup?

My setup includes a fresh Nest.js installation and a directory named "client" with a clean create-react-app installation inside. Here is the project structure: ./ ...some Nest.js folders... client <- React.js resides here ...some more Nest.js fo ...

Angular 4 - Unexpected path match failure triggered by route query parameters

After scouring through multiple discussions and questions regarding the different routing options in Angular 4, I have been unable to resolve an issue related to passing queryParams to an Angular 4 route. Whether passing them into the URL like this http: ...

Boost the Gen 2 User Directory

After reviewing all of the Amplify Gen 2 Documentation, I am struggling to find a way to display a list of registered users within my application. I need to create an admin page in Angular that will showcase all users along with their respective roles. I ...

Exploring the capabilities of Angular 10 in conjunction with threex.domevents.js

I'm experiencing difficulties with incorporating threex into my Angular project. Does anyone have experience using this JavaScript library in an Angular environment? ...

Angular 2 - Implementing click event functionality to add an item

I've searched for a solution to this before, but the best answer I could find was from 9 months ago: here and it doesn't work with the latest Angular version. Is there a way to add a new item (<li> or any other) to my HTML with a simple cl ...

Unable to locate 'http' in error handling service for Angular 6

My current project involves creating an Error Handling Service for an Angular 6 Application using the HTTP Interceptor. The main goal of this service is to capture any HTTP errors and provide corresponding error codes. However, my lack of familiarity with ...

Is there a way for the parent class to access the child class in Angular 2?

Seeking guidance on accessing a child class from a parent class in my Angular 2 application with Typescript. The structure of the parent and child classes are as follows: The parent class, AllDataPageComponent: @Component({ selector: 'all-data-p ...

Creating an Ionic 3 canvas using a provider

I recently followed a tutorial on integrating the canvas API into Ionic, which can be found at this link. However, I encountered an issue where all the canvas-related functions had to be placed within the pages class, making it quite cumbersome as these f ...

Angular Material Cards do not expand to fill the entire row

I recently started using Angular Material and I'm attempting to create a page with cards containing photos. However, it seems that by default, the mat-cards stack vertically and do not fill out the space in the row to the right. I've experimented ...

Eliminate unnecessary spacing from the sticky container

Trying to implement a sticky menu in an angular 14 project using angular material 14 has been quite challenging for me. Initially, I attempted to use the 'fixed' position, but encountered issues where the menu would consistently return to the to ...

Storing Images in MongoDB with the MEAN Stack: A Guide using Node.js, Express.js, and Angular 6

I am currently working on developing a MEAN Shop Application, and I have encountered an error while attempting to store the product image in MongoDB. typeError: cannot read the property 'productimage' of undefined Below is the route function fo ...

The Angular 2 router is not compatible with using the same component but with different IDs

Currently utilizing the alpha8 router with 3 main routes: export const appRoutes: RouterConfig = [ { path: '', component: LandingComponent }, { path: 'blog', component: BlogComponent }, { path: 'posts/:id', compon ...

SonarQube flagging a suggestion to "eliminate this unnecessary assignment to a local variable"

Why am I encountering an error with SonarQube? How can I resolve it since the rule page does not offer a specific solution? The suggestion is to eliminate the unnecessary assignment to the local variable "validateAddressRequest". validateAddress() { ...

What are some solutions to the error message "Error: Cannot find any matching routes" that appears when trying to switch between tabs following a successful login?

I am facing an issue with my Ionic 4 (4.10.2 with Angular 7.3.1) app where I want to make it accessible only after login. Following a tutorial from , I encountered a problem when trying to access the ion-tabs section of my app. Chrome keeps showing this er ...

The file isn't located in 'rootDir', even though all the details seem to be accurate

I'm currently troubleshooting an issue with one package in my nx monorepo that is causing the error code "TS6059". Interestingly, all other packages are functioning correctly in previous builds. After reviewing my index.ts files, it appears that all ...

Excluding files in Angular by adding them to the .gitignore list

Interested in mastering the integration of Twilio with your Angular app? The first step is to configure your Twilio credentials and make sure to add that file to the .gitignore. However, upon reviewing this example Angular-Twilio project on GitHub, I notic ...

Guide to displaying an input box depending on the selection made in a Mat-Select component

I am working on a mat-select component that offers two options: Individual Customers and Organizational Customers. When selecting Individual Customers, the dropdown should display three options: First Name, Last Name, and Customer Id. However, when choosin ...

Modify an array by incorporating values from another array that have a matching property

I have two arrays that look like this let array1 = [{ 'id': 1, 'name': 'A' }, { 'id': 2, 'name': 'B' }, { 'id': 3, 'name': ' ...

Guide to asynchronously loading images with Bearer Authorization in Angular 2 using NPM

I am in search of a recent solution that utilizes Angular2 for my image list. In the template, I have the following: <div *ngFor="let myImg of myImages"> <img src="{{myImg}}" /> </div> The images are stored as an array w ...

Issue with Angular 4 material: md-slider experiencing jerky sliding motion

In my Angular 4 application, I have implemented material design components using angular-cli. One specific component I am using is a md-slider. Although most of the functionality works correctly, there is an issue where the cursor does not move when the u ...