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

75
Vistas
Angular call function to capture div and wait for the cappture before continuing

I have a method that captures a div. I call this method called capture() from another method.

Here's the code:

theimage; // define the variable


callcapture() {

  // do stuff

  this.capture(); // Call the method here 

  // Do other stuff below BUT (Do not run the rest of the code until "this.capture() has finished ")

}


capture() {
  const element = document.getElementById("capture") as HTMLCanvasElement;
  html2canvas(element).then((canvas) => {
    this.theimage = canvas.toDataURL();
  });
}

How can I do this?

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

0

How about using Angular's @viewChild for that? It's rarerly a good idea to use native JS methods like getElementById, getElementsByClassName etc; in an Angular application.

    <canvas #myCanvas></canvas>
    @ViewChild('myCanvas', {static: false}) canvasElem: ElementRef;

And then you work with that :

const context = this.canvasElem.nativeElement.getContext("2d");
const base64:string = this.canvasElem.nativeElement.toDataURL();
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can return the promise and use another then to make sure the element has been captured already:

callcapture() {
  // do stuff
  this.capture().then(() => {
    // Do other stuff below BUT (Do not run the rest of the code until "this.capture() has finished ")
  });
}

capture() {
  const element = document.getElementById("capture") as HTMLCanvasElement;
  return html2canvas(element).then((canvas) => {
    this.theimage = canvas.toDataURL();
  });
}
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