/*
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();

}
}

最新文章

  1. ★Kali信息收集~3.子域名系列
  2. 学习笔记——SQLite介绍
  3. 使用jQuery加载script脚本
  4. PHP设计模式(一)
  5. Bean
  6. linux 常用的基本命令
  7. Spring shiro使用
  8. 夺命雷公狗ThinkPHP项目之----企业网站24之网站前台列表页面包屑导航的显示
  9. MongoDB备份与导入
  10. 从win7到mac os再到win10,体验总结
  11. JQ 一些基本方法
  12. SQL总结之对比和备份
  13. 《JAVASCRIPT高级程序设计》客户端检测
  14. suse安装svn服务端和客户端的使用
  15. 安装git,gitlab和TortoiseGit
  16. JVM内存异常与常用内存参数设置总结
  17. sql查询语句for xml path语法
  18. linux(centos 7)下安装elasticsearch - head插件(端口占用,防火墙关闭)
  19. centos 下python升级
  20. HDU - 4511 小明系列故事――女友的考验(AC自己主动机+DP)

热门文章

  1. C语言讲义——错误处理
  2. 题解 洛谷 P2612 【[ZJOI2012]波浪】DP+高精
  3. LeetCode 044 Wildcard Matching
  4. OllyDbg使用入门
  5. C#:终于有人把 ValueTask、IValueTaskSource、ManualResetValueTaskSourceCore 说清楚了!
  6. 定时器:Django-crontab
  7. 第7.27节 Python案例详解: @property装饰器定义属性访问方法getter、setter、deleter
  8. Python学习随笔:使用xlwings读取和操作Execl文件
  9. Project Lombok——带给你简洁、清晰的代码
  10. WPF页面刷新