基础

ViewChild

ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法。它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同类型的实例。比如ElementRef和ViewContainerRef.

ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素。视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函数中,就能正确获取查询的元素。

ViewChildren

ViewChildren 装饰器是用来从模板视图中获取匹配的多个元素,返回的结果是一个 QueryList 集合。

  1. Demo
  2. child.component.ts
  3. @Component({
  4. selector: 'app-child',
  5. template: '<p>{{name}}</p><div><input value="xixi"></div>',
  6. styleUrls: ['./child.component.css']
  7. })
  8. export class ChildComponent implements OnInit {
  9. public name: string = 'childName';
  10. greeting(name: string) {
  11. console.log('hello ' + name);
  12. }
  13. }
  14. app.component.ts
  15. @Component({
  16. selector: 'app-root',
  17. template: `<app-child #childA></app-child>
  18. <app-child #childB></app-child>
  19. <button (click)="clickMe()" >点我</button>`,
  20. })
  21. export class AppComponent implements OnInit {
  22. @ViewChild('childA')
  23. child1;
  24. @ViewChild(ChildComponent)
  25. child2;
  26. @ViewChild('childB', {read: ElementRef})
  27. child3;
  28. @ViewChild('childB', {read: ViewContainerRef})
  29. child4;
  30. @ViewChildren(ChildComponent)
  31. children;
  32. clickMe() {
  33. this.child1.greeting('child1');
  34. this.child2.name = '我是child2';
  35. this.child3.nativeElement.lastElementChild.firstElementChild.value = '我是child3~';
  36. this.child4._data.componentView.component.greeting('child4');
  37. this.children._results[0].greeting('children');
  38. }
  39. }

Angular ViewChild & ViewChildren的更多相关文章

  1. Angular ViewChild

    viewchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-component ...

  2. Angular 2 ViewChild & ViewChildren

    一.ViewChild ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素.视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函 ...

  3. Angular Viewchild undefined

    Angular的viewchild在使用的时候报错 undefined 1 检查是否在元素上打上标识 #xxx 2 查看引用元素时的时机 是否在AfterViewInit之后 3 检查元素是否在*ng ...

  4. Angular @ViewChild,Angular 中的 dom 操作

    Angular 中的 dom 操作(原生 js) ngAfterViewInit(){ var boxDom:any=document.getElementById('box'); boxDom.st ...

  5. angular ViewChild ContentChild 系列的查询参数

    官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...

  6. [Angular] @ViewChild read custom directive and exportAs

    For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...

  7. [Angular] ViewChild 'read' option

    It is not clear in the Docs about {read: xx} option for @ViewChild. Based on the Source code, @ViewC ...

  8. [Angular] @ViewChild and template #refs to get Element Ref

    We can use @ViewChild with component: @ViewChild(AuthMessageComponent) message: AuthMessageComponent ...

  9. Angular4学习笔记(七)- ViewChild和ViewChildren

    基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...

随机推荐

  1. 跟hzwer学分块!?

    orz hzwer.分块啥的真是太强了- (块状链表也真是太强了-) Upd:Upd:Upd: 好像链接时不时会炸?这个时候就体现了转载的作用 如果上面链接用不了就戳这里

  2. 视频断点续传+java视频

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  3. Neo4j 简介 2019

    Neo4j是一个世界领先的开源图形数据库,由 Java 编写.图形数据库也就意味着它的数据并非保存在表或集合中,而是保存为节点以及节点之间的关系. Neo4j 的数据由下面几部分构成: 节点边属性Ne ...

  4. 前端工程师需要掌握的 Babel 知识

    在前端圈子里,对于 Babel,大家肯定都比较熟悉了.如果哪天少了它,对于前端工程师来说肯定是个噩梦.Babel 的工作原理是怎样的可能了解的人就不太多了.本文将主要介绍 Babel 的工作原理以及怎 ...

  5. 关于使用express作为spa应用服务的问题

    前端工程师应该知道,spa是基于前端路由的单页面应用,如果服务端不做相应的配置,会经常出现404的问题. 一般的做法是默认返回应用的首页. express // 安装相关依赖 npm install ...

  6. 4.rabbitmq--路由模式

    rabbitmq--路由模式 想象之前的订阅发布模式: 一个生产者,多个消费者,每一个消费者都有自己的一个队列,生产者没有将消息直接发送到队列,而是发送到了交换机,每个队列绑定交换机,生产者发送的消息 ...

  7. FFmpeg之av_register_all()

    1. av_register_all() 该函数位于 libavformat/allformats.c 中. 该函数主要是注册所有的编解码器.复用/解复用组件等. /* * Initialize li ...

  8. Nslookup: command not found error on RHEL/CentOS 7

    Reference: https://unix.stackexchange.com/questions/164210/nslookup-command-not-found-error-on-rhel- ...

  9. 处理flutter http请求添加application/json报错Cannot set the body fields of a Request with content-type “application/json”

    在flutter中在http请求发送时设置"content-type": "application/json"会出现报错Cannot set the body ...

  10. laravel如何从mysql数据库中随机抽取n条数据

    laravel如何从mysql数据库中随机抽取n条数据 一.总结 一句话总结: inRandomOrder():$userQuestions=UserQuestion::where($map)-> ...