<h2>这是一个home组件--DOM操作演示</h2>
<div id="box">
this is box
</div>
<br>
<div id="box1" *ngIf="flag">
this is box1
</div>
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit { public flag:boolean=true; constructor() { } ngOnInit() { //组件和指令初始化完成 并不是真正的dom加载完成 let oBox:any=document.getElementById('box'); console.log(oBox.innerHTML);
oBox.style.color="red"; //获取不到dom节点
/*
let oBox1:any=document.getElementById('box1'); console.log(oBox1.innerHTML);
oBox1.style.color="blue"; */ } //视图加载完成以后触发的方法 dom加载完成 (建议把dom操作放在这个里面)
ngAfterViewInit(){
let oBox1:any=document.getElementById('box1');
console.log(oBox1.innerHTML);
oBox1.style.color="blue"; } }

效果:

最新文章

  1. Spring--开山篇
  2. 【康拓展开】及其在求全排列第k个数中的应用
  3. 高性能文件缓存key-value存储—Redis
  4. 【自动部署】Ansible 怎么通过堡垒机/跳板机 访问目标机器
  5. Maven-006-手动部署第三方构件至 nexus 私服
  6. [转]http://lua-users.org/wiki/LpegTutorial
  7. Python3 学习第十二弹: 补充something
  8. 【LeetCode】204 - Count Primes
  9. 转载 C#中静态类和非静态类比较
  10. ksh和bash区别
  11. 24种设计模式--组合模式【Composite Pattern】
  12. linux ulimit的使用,如何产生core文件,调试段错误
  13. mysql用户权限分配及主从同步复制
  14. linux系统做raid
  15. nodemanager 无法启动报错“doesn&#39;t satisfy minimum allocations”
  16. 开发第一个flutter程序 hello world
  17. 关于Javascript闭包(Closure)
  18. 为什么研发团队不适合量化KPI的绩效考核?
  19. Android的Fragment中onActivityResult不被调用
  20. HTML JavaScript 基础学习

热门文章

  1. NSIP
  2. STM32定时器配置(TIM1、TIM2、TIM3、TIM4、TIM5、TIM8)高级定时器+普通定时器,定时计数模式下总结
  3. about微信小程序
  4. Caused by: java.lang.ClassNotFoundException: org.fusesource.jansi.WindowsAnsiOutputStream
  5. ASP.NET MVC 入门5、View与ViewData
  6. DML子句returing into用法举例
  7. scoket --- 练习
  8. 16Vue 表单的输入绑定
  9. vue生命周期在工作中的用法
  10. PostgreSQL 不要使用kill -9 杀 Postgresq 用户进程