[Angular] Difference between ViewChild and ContentChild
*The children element which are located inside of its template of a component are called *view children *. On the other hand, **elements which are used between the opening and closing tags of the host element of a given component are called *content children **.
ViewChild:
Auth-form.component's template:
<div> <form (ngSubmit)="onSubmit(form.value)" #form="ngForm"> <ng-content select="auth-remember"></ng-content> <auth-message></auth-message> </form> </div>
Here we can use ViewChild to access <auth-message></auth-message> component, because it is a child component of auth-form component.
ContentChild:
auth-form component:
<auth-form (submitted)="loginUser($event)"> <auth-remember [role]="'checkbox1'" (checked)="rememberUser($event)"> </auth-remember> </auth-form>
Notice here <auth-remember> is passed into <auth-form> by content projection.
<form (ngSubmit)="onSubmit(form.value)" #form="ngForm"> <ng-content select="auth-remember"></ng-content> <auth-message></auth-message> </form> </div>
So here if we want to access <auth-remeber> inside auth-form component, we have to use @ContentChild.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-02-19 [Immutable + AngularJS] Use Immutable .List() for Angular array
2016-02-19 [Protractor] Running tests on multiple browsers
2016-02-19 [Protractor] Protractor Interactive with elementor