Video summary
ngAfterContentInit vs ngAfterContentChecked in angular | Component Lifecycle hooks
Main summary
Key takeaways
Main ideas / concepts conveyed
Angular component lifecycle hooks (focused on projected content)
The video focuses on two lifecycle hooks:
ngAfterContentInit- Runs once after the projected content is initialized.
ngAfterContentChecked- Runs whenever the projected content changes (and is checked again by Angular).
Content projection in Angular
Content projection allows a parent component to pass dynamic content into a child component.
- The child uses
<ng-content>as a placeholder for whatever the parent supplies. - The projected content can be:
- Simple HTML
- Complex HTML
- Even another component (e.g., an “address” component)
How hooks relate to content projection
In the child component (e.g., Authors), the projected content may not always be available immediately.
- Use
ngAfterContentInit- For logic that depends on content being initialized.
- Use
ngAfterContentChecked- For logic that must respond to content changes.
Using @ContentChild to access projected component/data
When the parent projects a component (not just HTML), the child can access it using @ContentChild(...).
Example described:
- The projected component is
AuthorsAddress - The child (
Authors) uses@ContentChild(AuthorsAddress)to get a reference - The child then reads something like
this.address?.property(using optional chaining) and uses it in its logic
Note: The video highlights that
ngAfterContentInitmay run before the projected reference is available in some scenarios, whilengAfterContentCheckedwill see the updated content reliably after it exists.
Method / instruction steps shown
1) Set up the parent-child structure for content projection
- Open the Angular bookstore app and run it with:
ng serve
- Identify:
- Parent component:
Home - Child component:
Authors
- Parent component:
- Use the child component inside the parent conceptually like:
<app-authors>...</app-authors>
- In the child component template, add:
<ng-content>to mark where parent-provided content should render.
2) Demonstrate projecting different content from the parent
- First example: parent projects an
h1element with text- The child displays that text where
<ng-content>is placed.
- The child displays that text where
- Second example: parent projects a
button(or other HTML)- The child displays that button as well.
3) Implement lifecycle hooks in the child to observe behavior
In the child component (Authors), implement:
ngAfterContentInit- Add
console.log(...)to confirm when it fires.
- Add
ngAfterContentChecked- Add
console.log(...)to confirm when it fires.
- Add
Observe:
- If the child component appears multiple times in the parent, the hooks run for each instance.
4) Make the projected content a component (not only HTML)
- Create a new component:
AuthorsAddress(via Angular CLI; described asng g c ...)
- Declare and export it in the shared module:
- Add it to
declarations - Add it to
exports
- Add it to
- In
AuthorsAddress:- Use an
@Input()property to receive data - Display that input data in its template
- Use an
-
Project
AuthorsAddressfromHomeintoAuthors:- Parent conceptually uses something like:
<app-authors-address [inputProp]="...">...</app-authors-address>
- Parent conceptually uses something like:
-
Confirm:
- The projected component renders inside the child’s
<ng-content>slot.
- The projected component renders inside the child’s
5) Access the projected component from inside the child using @ContentChild
In the Authors child component:
- Add
@ContentChild(AuthorsAddress)- Assign the reference to a property such as
address
- Assign the reference to a property such as
- Use safe access (optional chaining) to avoid undefined errors:
- Example approach:
this.address?.<value>
- Example approach:
Observe:
ngAfterContentInitmay run before the reference exists in some scenarios (video notes it can beundefinedfor the first instance).ngAfterContentCheckedsees the data reliably after content exists.
6) Demonstrate hook firing conditions by changing projected content
- Simplify to a single
Authorsusage so outputs are clearer. - Introduce a dynamic value in the parent:
- A string property like:
public address = 'india' - A method (e.g., a counter or click handler) that changes
address
- A string property like:
- Project content in a way that uses
address, so updates change what’s projected. - Observe:
- When content changes:
ngAfterContentCheckedruns (often repeatedly)
- When content is first initialized:
ngAfterContentInitruns once
- When content changes:
Final observation stated:
ngAfterContentInitonly runs once after initial content is setngAfterContentCheckedruns on subsequent content changes/checks
Speakers / sources featured
- No specific person is named in the subtitles.
- Source: An educational YouTube video titled “ngAfterContentInit vs ngAfterContentChecked in angular | Component Lifecycle hooks” with an unnamed presenter.