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

503
Vistas
method that uses data retrieved from an observable on its own component returns undefined when used as Input()

I created a template example.component.html that receives a method as a variable to its click action:

<div>
  <button (click)="method()">click here</button>
</div>

on the example.component.ts file, the method comes from an input(), so I can use this template on multiple situations:

@Component({
  selector: 'example',
  templateUrl: './example.component.html',
})
    export class ExampleComponent implements OnInit {
      @Input() method;
    
      constructor() {}
    
      ngOnInit(): void {}
    }

Here's where it gets complicated. At the parent component, the method that will be triggered on click uses a variable coming from an observable:

parent-example.component.html

  <example [method]="onClick"></example>

parent-example.component.ts

@Component({
  selector: 'parent-example',
  templateUrl: './parent-example.component.html',
})
export class ParentExampleComponent implements OnInit {
  @Input() method;
  business;

  constructor(businessEntityService: BusinessEntityService) {
    businessEntityService.entities$.subscribe(
      data => (this.business = data),
    );
  }

  onClick() {
    console.log(this.business);
  }

  ngOnInit(): void {}
}

Even though the parent component is subscribing businessEntityService observable and I've check it has in fact data, when I click the button, the console logs undefined.

I understand this probably has to do with scope and the stack is looking for this.business at the children component, however I would love to know if there's anyway to call a method that uses a variable from a subscription from its own component as Input().

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The this context is being lost (I think). Can happen when passing class methods as params

Replace the ParentExampleComponent#onClick method with:

onClick = () => {
    console.log(this.business);
}

Note: behind the scenes, Typescript will now treat onClick as a class property, and moves that code into the constructor. Using an arrow function locks the this context of that function

over 4 years ago · Santiago Trujillo 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