基础

ViewChild

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

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

ViewChildren

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

Demo
child.component.ts
@Component({
selector: 'app-child',
template: '<p>{{name}}</p><div><input value="xixi"></div>',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
public name: string = 'childName'; greeting(name: string) {
console.log('hello ' + name);
}
}
app.component.ts
@Component({
selector: 'app-root',
template: `<app-child #childA></app-child>
<app-child #childB></app-child>
<button (click)="clickMe()" >点我</button>`,
})
export class AppComponent implements OnInit {
@ViewChild('childA')
child1;
@ViewChild(ChildComponent)
child2;
@ViewChild('childB', {read: ElementRef})
child3;
@ViewChild('childB', {read: ViewContainerRef})
child4; @ViewChildren(ChildComponent)
children; clickMe() {
this.child1.greeting('child1');
this.child2.name = '我是child2';
this.child3.nativeElement.lastElementChild.firstElementChild.value = '我是child3~';
this.child4._data.componentView.component.greeting('child4');
this.children._results[0].greeting('children');
}
}

最新文章

  1. for+next()实现数组的遍历及while list each 的使用
  2. wpf 自定义圆形按钮
  3. [php/html/CSS]给Aptana3 安装 Emmet插件
  4. Crawler4j学习笔记
  5. 使用Servlet Filter做Login checking
  6. Windows环境变量设置无效解决办法——DOS窗口设置环境变量
  7. 杭电ACM减花布条
  8. packinfo-java的作用
  9. jQuery移除指定元素后的所有元素
  10. URAL 1225 Flags
  11. Apache 实现ProxyPass转发URL到Tomcat并实现http自动转https【转载】
  12. 201521123089 《Java程序设计》第3周学习总结
  13. js 重写alert 兼容iphone使得alert 不带src
  14. ssm框架的整合搭建(二)
  15. cas4.2.4 登添加验证码
  16. 对 Phantomjs / CasperJS 进行远程调试
  17. Django2.0 URL配置
  18. Pecan
  19. jquery 部分函数源码解析
  20. Xcode-push到远程仓库不能使用邮箱名,需使用昵称

热门文章

  1. DockerAPI版本不匹配的问题
  2. 收藏一个ST表模板
  3. [Luogu] 子串
  4. [mysql]查看mysql执行情况的几种方法
  5. AtCoder AGC001E BBQ Hard (DP、组合计数)
  6. D. Shortest Cycle(floyd最小环)
  7. Mybatis源码学习之整体架构(一)
  8. Nginx访问非常慢
  9. mitmproxy修改二级代理
  10. docker部署TX-LCN5.0.2