首页 > 其他 > 详细

angular8 大地老师学习笔记---第八课

时间:2019-12-05 20:44:10      阅读:126      评论:0      收藏:0      [点我收藏+]
/*
ViewChild获取dom节点

1、模板中给dom起一个名字
<div #myBox>
我是一个dom节点
</div>

2、在业务逻辑里面引入ViewChild
import { Component, OnInit,ViewChild} from ‘@angular/core‘;

3、 写在类里面 @ViewChild(‘myBox‘) myBox:any;

4、ngAfterViewInit生命周期函数里面获取dom
this.myBox.nativeElement



*/


import { Component, OnInit,ViewChild} from ‘@angular/core‘;

@Component({
selector: ‘app-news‘,
templateUrl: ‘./news.component.html‘,
styleUrls: [‘./news.component.scss‘]
})
export class NewsComponent implements OnInit {

//获取dom节点
@ViewChild(‘myBox‘) myBox:any;

//获取一个组件
@ViewChild(‘header‘) header:any;

constructor() { }

ngOnInit() {}

ngAfterViewInit(): void {

console.log(this.myBox.nativeElement);

this.myBox.nativeElement.style.width=‘100px‘;

this.myBox.nativeElement.style.height=‘100px‘;

this.myBox.nativeElement.style.background=‘red‘;

console.log(this.myBox.nativeElement.innerHTML);

}


getChildRun(){
//调用子组件里面的方法
this.header.run();

}
}

angular8 大地老师学习笔记---第八课

原文:https://www.cnblogs.com/fpcing/p/11991682.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!