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

99
Vistas
Why is this property showing up undefined in angular?

I can't seem to figure out why the first console.log is displaying the parameter correctly but the second shows undefined.

Console Log Output:

text
undefined

Code:

ngOnInit() {
this.getPuzzle();
}
    getPuzzle() {
    this.restProviderService.getPuzzle(this.gameService.getGameId()).subscribe(puzzle => {
      this.puzzleType = puzzle.type;
      this.puzzleValue = puzzle.value;

      console.log(this.puzzleType);

      setTimeout(this.handlePuzzle , 3000);
    });
  }
    
      handlePuzzle() {
          console.log(this.puzzleType);
          if (this.puzzleType == 'text') {
            new Vara("#text", "assets/fonts/vara/Satisfy/SatisfySL.json", [{
              text: 'hello',
              delay: 2000,
              x: 2
            }], {
              fontSize: 25,
              strokeWidth: 1.5,
              duration: 5000,
              color: "black"
            });
          }
        
    
      }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

When you pass the function this.handlePuzzle to setTimeout the this representing your class is lost, and handlePuzzle receives the setTimeout's this instead.

Use an arrow function instead of passing the function itself:

setTimeout(() => this.handlePuzzle(), 3000);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Inside JS event listeners this refers to the event target. But inside the handlePuzzle method (sort of an event listener), you need this to refer to the current object instance (as usual in a method).

You have 2 solutions:

  • Use an arrow callback. Arrow callbacks preserve the value of this.

    setTimeout(()=>this.handlePuzzle(), 3000)

    or in your method declaration

    handlePuzzle=()=> { console.log(this.puzzleType);

  • Use bind to set the value of this.

    setTimeout(this.handlePuzzle.bind(this), 3000);

    or, in the constructor

    this.handlePuzzle = this.handlePuzzle.bind(this);

For more details, refer to the following article. https://dev.to/alexdevero/a-quick-guide-to-this-keyword-in-javascript-what-this-is-and-when-7i5

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