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

69
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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