Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
How to use variable outside of ngOnInit

I have the following code in my ngOnInit:

ngOnInit(): void {
let selectedArea:any = null;
let areas = document.querySelectorAll<SVGElement>('path'); // gets an array of all of the paths in the DOM
areas.forEach((area) => {      // for each svg path
   area.addEventListener('mouseover', function () {
     area.style.fill = '#2100b6'; // add hover
    });
    area.addEventListener('mouseout', function () {
      area.style.fill = ''; // return to default on mouse out
     });
     area.addEventListener('click', function () {
       if (selectedArea) {
         area.style.fill="black";
       }
       if (selectedArea !== area.id) {
         selectedArea = area.id;
         area.setAttribute('class', 'selectedArea'); // changed
         area.style.fill="black";
       }
        console.log(selectedArea);
      });
      this.selectedRegion = selectedArea
    }); 
} 



selectedRegion : number = 0

"selectedArea" logs the selected region's code from an SVG map. I tried to get that into the new variable "selectedRegion", but it did not work.

So, I want to use "selectedArea" outside of ngOninit, but I don't have any idea, how to do it.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda