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

100
Vistas
Trigger function on field change

I want to trigger a function if a flag variable is changed. I read about ngOnChanges only works with @Input whereas I have a variable.

isFormCompleted: boolean = false;

ngOnInit() {
  this.checkComplete();
}

checkComplete() {
  /**
  * Checks if all conditions are valid and then turns the flag variable to true
  **/
  this.isFormCompleted = true;
}

Now I want to fire a function as soon as the flag variable is changed to true. How do I achieve that?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Using getters and setters:

private _isFormCompleted: boolean = false;

get isFormCompleted(): boolean {
  return this._isFormCompleted;
}

set isFormCompleted(value: boolean) {
  this._isFormCompleted = value;
  if (this._isFormCompleted) {
    this.functionCall();
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use rxjs for that, creating a Subject which is a stream that you can subscribe on when it changes. Angular should have the packages by default

isFormCompleted$: Subject = new Subject<boolean>(false);

ngOnInit() {
  this.isFormCompleted$.subscribe({
    // Here you can add your callback and do with the changing value whatever you want
    next: (v) => console.log(`value: ${v}`)
  });
}

checkComplete() {
  this.isFormCompleted$.next(true);
}

This will always be executed when the subject changes its value. You can trigger it by calling:

this.isFormCompleted$.next(true);

Documentation: https://rxjs.dev/guide/subject

about 4 years ago · Juan Pablo Isaza Denunciar

0

You may try this way also.

isFormCompleted: boolean = false;

ngOnInit() {
  this.checkComplete();
}

checkComplete() {

  if (this.isFormCompleted) {
      this.isFormCompleted = !this.isFormCompleted
   }
     
}
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