Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
¿Hay una forma angular de hacerlo: document.getElementById(id).style.display = "none";

Tengo un div con la identificación de 1. Estoy tratando de configurar la pantalla en ninguno dinámicamente. ¿Hay una forma angular de hacer esto? Actualmente, estoy usando vainilla javascript. Estaba preguntando acerca de hacer esto dinámicamente porque habrá más de 60 divs que se crearán a partir de una matriz.

en mi html

 <div *ngFor="let item of items; i = index;"> <div id={{i}} (click)=hideDiv()></div> </div>

en mi método

 hideDiv() { return document.getElementById('1').style.display = "none"; }

Eso funciona, pero estoy buscando la forma Angular de hacer lo anterior.

Me sugirieron que use @ViewChild. Esto es lo que he cambiado. No puedo usar una variable de referencia de plantilla ya que los divs html se crean dinámicamente. A menos que alguien pueda decirme cómo crear dinámicamente las variables de la plantilla. Aunque no creo que sea posible crear variables de plantilla con un bucle.

 @ViewChild('imgId', { static: true }) elementRef: ElementRef<HTMLDivElement>; imgId: string;

Luego en el método tengo:

 this.imgId = event.path[0].attributes[1].value; this.elementRef.nativeElement.style.display = "none";

El event.path[0].attributes[1].value me da la identificación de la imagen. El imgId se muestra cuando lo registro en la consola. Todavía no cambia la visualización en el div a ninguno. También recibo el error: No se pueden leer las propiedades de undefined (leyendo 'nativeElement')

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Sí, puede usar la consulta ViewChild en Angular para hacer esto. En su componente, defina una consulta como esta:

 @ViewChild('#1') elementRef: ElementRef<HTMLDivElement>;

Implemente la interfaz AfterViewInit en su componente y, dentro de ella, use esto:

 this.elementRef.nativeElement.style.display = "none";
about 4 years ago · Santiago Gelvez Report

0

Simplemente puede usar ngIf para esto

Componente

 shouldDisplay: boolean = true; hide(): void { this.shouldDisplay = false; } show(): void { this.shouldDisplay = true; }

html

 <button (click)="hide()">Hide</button> <button (click)="show()">Show</button> <div *ngIf="shouldDisplay">this is the content</div>

Aquí está el ejemplo de trabajo

about 4 years ago · Santiago Gelvez Report

0

Esta es la forma Angular: plantilla

 <div *ngIf="showMe"></div>

o

 <div [hidden]="!showMe"></div>

Mecanografiado:

 showMe: boolean; hideDiv() { this.showMe = false; }
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!