Tengo problemas para acceder a un elemento en una lista de ElementRefs en Angular. Quiero recorrer la lista completa de ellos y actualizar sus propiedades CSS en consecuencia.
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { @ViewChild('div') my_divs:QueryList<ElementRef>; constructor() { } ngAfterViewInit() { } do_something() { for(var i=0; i<this.my_divs.length; i++){ //THIS IS WRONG this.my_divs[i].nativeElement.style.background_color = "red"; } } }Sin embargo, me encuentro con este error:
Element implicitly has an 'any' type because type 'QueryList<ElementRef<any>>' has no index signature. Did you mean to call 'get'?Pero cuando cambio la indexación a un get, posiblemente no esté definido ...
Cambié el decorador a ViewChildren pero también agregué esto:
@ViewChildren('div', {read: ElementRef}) my_divs:QueryList<ElementRef>; Luego pude cambiar el bucle for a un forEach