浅谈Angular中的DOM操作


当前第2页 返回上一页

1

2

3

<div #myBox>

   我是一个dom节点

</div>

2、现在组件ts通过ViewChild 获取dom

1

2

3

4

<div #myBox>我是一个dom节点</div>

<app-header #header></app-header>

<button type="button" (click)='getChildProp()'>获取子组件header的属性</button>

<button type="button" (click)='getChildMethod()'>获取子组件header的方法</button>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

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

 

@Component({

  selector: 'app-news',

  templateUrl: './news.component.html',

  styleUrls: ['./news.component.less']

})

export class NewsComponent implements OnInit {

  //获取Dom

  @ViewChild('myBox')

  public myBoxIn: any;

 

  @ViewChild('header')

  public header: any;

 

  constructor() { }

 

  ngOnInit(): void {

    // console.log(this.myBoxIn)

 

  }

 

  //处理dom节点

  ngAfterViewInit() {

    console.log(this.myBoxIn.nativeElement)

 

    //父组件获取到了整个子组件header

    console.log('父组件获取到了整个子组件header')

    console.log(this.header)

  }

  //获取子组件header的属性

  getChildProp() {

    console.log(this.header.title)

 

  }

  //获取子组件header的方法

  getChildMethod() {

    console.log(this.header.headRun)

    this.header.headRun();

  }

 

}

 

 

// 父组件   news   引入 <app-header #header></app-header>

// 子组件  header

 

// 父组件 得到 子组件的 数据 和 方法   ---   子组件 传 值给父组件 

 

 

// 总结:

// 1. 父组件中调用子组件的时候, 给子组件一个名称

// <app-header #header></app-header>

// 2. 在父组件引入viewChild

 

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

 

 

// @ViewChild('header')

// public header:any;

 

// 3. 已经可以在父组件调用子组件的属性和方法了

 

 

// 父组件传值给子组件  @input   -- 子组件 得到 父组件的 数据 和 方法

 

// 父组件: home

// 子组件: header

更多编程相关知识,请访问:编程入门!!

以上就是浅谈Angular中的DOM操作的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

Angular中关于单元测试的面试题,你能回答上来吗?

详解Angular中自定义创建指令的方法

Angular和vue.js的区别是什么?Angular和vue.js的深度对比

浅谈Angular中父子组件间怎么传递数据

10个从喜到悲的Angular面试题

浅谈Angular中控制器、服务和指令的关系

Angular cli如果搭建Angular+typescript+material项目?

详解Angular中jsencrypt插件的使用方法

浅谈Angular中的component/service

浅谈Angular中的模块(ngmodule),延迟加载模块

更多相关阅读请进入《Angular》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...