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

247
Vistas
The elementId is null after the second time the component has been initialized

I have a component where I will draw a canvas chart. And when I initialize the component for the first time, everything is ok, but if I leave this component and return to it again, then document.getElementById('myChart'); is null and doesn't draw my chart

    ngAfterViewInit() {
    Chart.register(...registerables);
    this.Chart(); 
}


private Chart() {
    const ctx: any = document.getElementById('myChart');
const myChart = new Chart(ctx.getContext('2d'), { // some shit })
}

I tried to do it differently

 const canvas = <HTMLCanvasElement>document.getElementById('myChart');
    const ctx = canvas.getContext('2d');

But I didn't quite understand how to solve this error and eventually came to the first option

Argument of type 'CanvasRenderingContext2D | null' is not assignable to parameter of type 'ChartItem'.   Type 'null' is not assignable to type 'ChartItem'.
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I approached this problem from a different angle. First I used ViewChild and applied ElementRef to it

  @ViewChild('myChart') chartRef: ElementRef;
  private myChart: Chart;

And be sure to use everything in ngAfterViewInit since it starts working after building the DOM tree

  ngAfterViewInit(): void {
    this.Chart();
  }

 this.myChart = new Chart(this.chartRef.nativeElement, {some shit})
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