Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

903
Vistas
Angular - How to create global functions?

I just started learning Angular and it seems a bit different to React which was the first JS framework/library I learned.

I watched some courses this week and I thought I should write down some code and see how far it gets me, test what I've learned. You might recognize the app as it's been one of the challenges listed on Frontend Mentor and I've completed this in React so I wanted to give it a go in Angular aswell.

This is what I've done so far. https://i.imgur.com/HbrndxR.png

Atm, I am not fetching data from the API because I don't know how, just yet. However, what I want to do is:

  1. when I click the Light button, the theme should switch to Dark and vice-versa. I created a service for this.
export class ThemeService {
  isLightTheme:boolean = true;

  changeTheme():void {
    this.isLightTheme = !this.isLightTheme;
  }
}

What I did was to link up the button in the header to the changeTheme() function

TS

  changeTheme():void {
    this.themeService.changeTheme();
    this.isLightTheme = this.themeService.isLightTheme;    
  }

HTML

<button (click)="changeTheme()">...</button>

But I am not sure how to listen to changes of this isLightTheme property in the service and apply it to other components that need to know which theme to apply.

  1. How can I create the functionality that when there's something typed in the search input, the country cards get filtered? Also a service? How am I supposed to link up two different components (search input is a component, the cards container is a different input)? Observables?
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The best way to do this is to use BehaviourSubject from the rxjs lib

The code would look like this:

export class ThemeService {

  public isLightTheme: BehaviorSubject<boolean> = new BehaviorSubject(true);

  changeTheme():void {
    this.isLightTheme.next(!this.isLightTheme.getValue());
  }
}

Then, in your components, you can subscribe to the value of isLightTheme like this:

themeService.isLightTheme.subscribe(val=>{
   //do something with val
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Q1: You can use localStorage.

to set the value:

localStorage.setItem("isLightTheme",true);

to get the value

this.isLightTheme = localStorage.getItem("isLightTheme");

Reference 1 Reference 2

Q2: You can use HTMLElement to hide the div (I'm assuming that you are assigning an id to the country card divs).

(<HTMLElement>document.getElementById('your-div-id')).<attribute-to-change>= newValue;

Reference 3

about 4 years ago · Juan Pablo Isaza Denunciar

0

Solution 1: The best answer is to use NgRX state management to store this type of information. which can be shared in other parts of the application. Any update in one place can update the whole application at once.

Solution 2: Use @Input and @Output decorators to share information between components,
you can see an example here https://indepth.dev/posts/1218/lets-implement-a-theme-switch-like-the-angular-material-site

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda