angular - Render is not able to changed the color of back ground
问题描述
export class ParentComponent implements OnInit {
constructor(private render : Renderer2) { }
@ViewChild('box', { static: false }) box: ElementRef;
ngOnInit(): void {
this.render.setStyle(this.box.nativeElement,'backgroundColor','red'); }
}
<div #box>
<p > from rendere</p>
</div>
core.js:6228 ERROR TypeError: Cannot read property 'nativeElement' of undefined
I am trying to change the colour of div by reader following is not able to change it. accessing the div by the template ref nad view child
解决方案
View queries are set before the ngAfterViewInit
callback is called. Check please link
export class ParentComponent implements OnInit,AfterViewInit{
constructor(private render : Renderer2) { }
@ViewChild('box', { static: false }) box: ElementRef;
ngOnInit(): void {}
ngAfterViewInit(){
this.render.setStyle(this.box.nativeElement,'backgroundColor','red');
}
}
推荐阅读
- excel - Workbooks.add 故障 1004
- android - Android 通知未在指定时间和日期显示
- r - .csv 文件大小差异很大
- java - com.fasterxml.jackson.databind.node.IntNode 不能转换为 com.fasterxml.jackson.databind.node.ObjectNode
- google-maps-api-3 - 如何在 Google Maps v3 中旋转包含 GroundOverlay 的 KML 图层
- python - 如何让这段代码更简洁易读
- sql - “With”命令比使用临时表慢 Select * into #table1 from
- react-native - 在图像的上角放置一个“x”,用于删除(React Native)
- python - Python 调用当前目录
- javascript - 如何使用 javascript 的 kubernetes-client 编写 CRD?