我在 Angular 中訪問 ElementRefs 串列中的元素時遇到問題。我想遍歷它們的整個串列并相應地更新它們的 CSS 屬性。
@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";
}
}
}
但是我遇到了這個錯誤:
Element implicitly has an 'any' type because type 'QueryList<ElementRef<any>>' has no index signature. Did you mean to call 'get'?
但是當我將索引更改為 get 時,我可能會不確定...
uj5u.com熱心網友回復:
我確實將裝飾器更改為 ViewChildren 但也添加了以下內容:
@ViewChildren('div', {read: ElementRef}) my_divs:QueryList<ElementRef>;
然后我能夠將for回圈更改為forEach
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/341506.html
標籤:javascript html 有角的 dom 网络前端
上一篇:如何在Node.js中決議和修改XHTML(支持HTML物體和CDATA部分)?
下一篇:單擊按鈕后如何從按鈕中獲取值
