ViewChild与ContentChild的联系和区别
原文
https://www.jianshu.com/p/5ab619e576ea
大纲
1、认识ViewChild
2、认识ContentChild
3、ViewChild与ContentChild的联系和区别
认识ViewChild
/*
angular中文文档的说明
You can use ViewChild to get the first element or the directive
matching the selector from the view DOM. If the view DOM changes,
and a new child matches the selector, the property will be updated. View queries are set before the ngAfterViewInit callback is called. Metadata Properties:
1、selector - the directive type or the name used for querying.
2、read - read a different token from the queried elements. 翻译:
你可以使用ViewChild去获得从DOM视图中通过选择器匹配到的第一个元素或者指令。
如果这个DOM视图发生变化,那么对应匹配到的ViewChild也会跟着更新。(更新未实现)
视图的获取是在ngAfterViewInit的时候
元数据匹配:
1、选择器:指令类型(包括组件)
2、根据不同的token获取节点元素(token指模板变量,如#child) 通俗翻译:
可以通过ViewChild这个装饰器获取视图元素中的内容,
这个视图包括视图模板中的视图元素,也包括作为子组件放置在视图模板中的内容。
ViewChild装饰器不可以在constructor的时候注入,
因为ViewChild装饰器获取元素是在ngAfterViewInit回调前被调用,
即在ngAfterViewInit中才实现
ViewChild装饰器获取目标元素的方式有两种:
1、一种是通过在模板元素中存在的子组件的组件名,如:ChildComponent
2、一种是通过获取模板元素的模板变量如#child来指定获取
*/
认识ContentChild
/*
angular中文文档:
You can use ContentChild to get the first element or the directive
matching the selector from the content DOM. If the content DOM changes,
and a new child matches the selector, the property will be updated. Content queries are set before the ngAfterContentInit callback is called. Metadata Properties:
1、selector - the directive type or the name used for querying.
2、read - read a different token from the queried element. 翻译:
你可以使用ContentChild去获得从DOM视图中通过选择器匹配到的第一个元素或者指令。
如果内容DOM发生变化,并且新的子项与选择器匹配,则属性将被更新。
内容查询发生在ngAfterContentInit回调前,即contentChild的获取发生在生命周期
ngAfterContentInit。 元数据属性:
1、选择器——用于查询的指令类型或名称。
2、从查询元素读取不同的令牌。
*/
ViewChild与ContentChild的联系和区别
/*
ViewChild和ContentChild其实都是从子组件中获取内容的装饰器 它们本质的区别其实就只是在于方法调用的时机以及获取内容的地方: 1、时机:
ViewChild在ngAfterViewInit的回调中调用
ContentChild在ngAfterContentInit的回调用调用 2、获取内容的地方
ViewChild从模板中获取内容
ContentChild需要从ng-content中投影的内容中获取内容,
也就是没有使用ng-content投影就无法获取内容
*/
ViewChild与ContentChild的联系和区别的更多相关文章
- Angular6 学习笔记——内容投影, ViewChild和ContentChild
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- [Angular] Difference between ViewChild and ContentChild
*The children element which are located inside of its template of a component are called *view child ...
- angular2 学习笔记 ( Component 组件)
refer : https://angular.cn/docs/ts/latest/guide/template-syntax.html https://angular.cn/docs/ts/late ...
- angular2的模板语法
Angular 应用管理着用户之所见和所为,并通过 Component 类的实例(组件)和面向用户的模板来与用户交互. 从使用模型-视图-控制器 (MVC) 或模型-视图-视图模型 (MVVM) 的经 ...
- angular 模板语法(官方文档摘录)
https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...
- Angular 4+ 修仙之路
Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...
- Angular6 学习笔记——指令
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- Angular6 学习笔记——组件详解之组件通讯
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- Angular6 学习笔记——组件详解之模板语法
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
随机推荐
- Java Reflection - Getters and Setters
原文链接:http://tutorials.jenkov.com/java-reflection/getters-setters.html 通过使用 Java 反射,我们能够在程序执行时观察 clas ...
- xampp 在 centos 中无法启动的解决办法
修改这个文件 vim /opt/lampp/lampp 找到 if egrep "9 " /etc/redhat-release > /dev/null更改为 if ...
- Android开发经验之获取画在画布上的字符串长度、宽度(所占像素宽度)
Android中获取字符串长度.宽度(所占像素宽度) 计算出当前绘制出来的字符串有多宽,可以这么来! 方法1: Paint paint = new Paint(); Rect rect = new R ...
- Android ViewPager嵌套ViewPager滑动冲突处理方法
dispatchTouchEvent方法用于事件的分发,Android中所有的事件都必须经过这个方法的分发, 然后决定是自身消费当前事件还是继续往下分发给子控件处理.返回true表示不继续分发,事件没 ...
- 洛谷 P1724 东风谷早苗
P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧 ...
- Arch Linux实体机安装记录
下面将记录笔者在戴尔笔记本安装arch linux的过程,用于记录,以便下次使用. 本文的内容参考arch linux官方Wiki. 首先,使用Power ISO把镜像安装到U盘,使用U盘安装. 通过 ...
- Netty推荐addListener回调异步执行
说明 Netty推荐使用addListener的方式来回调异步执行的结果,这种方式优于Future.get,能够更精确地把握异步执行结束的时间. 错误理解使用addListener的方式 代码如下: ...
- 00103_死锁、Lock接口、等待唤醒机制
1.死锁 (1)同步锁使用的弊端:当线程任务中出现了多个同步(多个锁)时,如果同步中嵌套了其他的同步.这时容易引发一种现象:程序出现无限等待,这种现象我们称为死锁.这种情况能避免就避免掉: synch ...
- EXT gridPanel 添加图片
var workAreaGrid = new Ext.grid.GridPanel({ region: 'west', title: '工作面预警结果', store: wkSto, width: , ...
- Mysql从入门到精通整理
目录 mysql基础 mysql进阶 mysql高级 mysql优化 正文 数据库是信息化产业的最基础的软件之一,各种管理系统,网站,在线游戏,背后基本都会有数据库的支持. 回到顶部 mysql基础 ...