DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Angular Lifecycle Hooks

Angular Lifecycle Hooks

Learn more about each phase of the angular life-cycle with examples.

Ilyoskhuja Ikromkhujaev user avatar by
Ilyoskhuja Ikromkhujaev
·
Dec. 18, 21 · Web Dev Zone · Analysis
Like (1)
Save
Tweet
3.00K Views

Join the DZone community and get the full member experience.

Join For Free

The angular application goes through a whole arrangement of cycles or has a lifecycle right from its introduction to the furthest limit of the application. The portrayal of lifecycle in pictorial portrayal is as follows,

Lifecycle hooks angular

The description of each lifecycle method is as below,

ngOnChanges

A callback method that is invoked immediately after the default change detector has checked data-bound properties if at least one has changed, and before the view and content, children are checked.

interface OnChanges {
  ngOnChanges(changes: SimpleChanges): void
} 

The following snippet shows how a component can implement this interface to define an on-changes handler for an input property.

TypeScript
 
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnChanges {
  @Input() prop: number = 0;

  ngOnChanges(changes: SimpleChanges) {
    // changes.prop contains the old and the new value...
  }
}

ngOnInit 

This is called whenever the initialization of the directive/component after Angular first displays the data-bound properties happens.

TypeScript
 
interface OnInit {
  ngOnInit(): void
}   

The following snippet shows how a component can implement this interface to define its own initialization method.

TypeScript
 
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnInit {
  ngOnInit() {
    // ...
  }
}

ngDoCheck

This is for the detection and to act on changes that Angular can't or won't detect on its own.

interface DoCheck {
  ngDoCheck(): void
} 

The following snippet shows how a component can implement this interface to invoke it own change-detection cycle.

TypeScript
 
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements DoCheck {
  ngDoCheck() {
    // ...
  }
}

ngAfterContentInit

This is called in response after Angular projects external content into the component's view.

      
interface AfterContentInit {
  ngAfterContentInit(): void
}
    

The following snippet shows how a component can implement this interface to define its own content initialization method.

TypeScript
 
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements AfterContentInit {
  ngAfterContentInit() {
    // ...
  }
}

ngAfterContentChecked

This is called in response after Angular checks the content projected into the component.

interface AfterContentChecked {
  ngAfterContentChecked(): void
}  

The following snippet shows how a component can implement this interface to define its own after-check functionality.

TypeScript
 
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements AfterContentChecked {
  ngAfterContentChecked() {
    // ...
  }
}

ngAfterViewInit

This is called in response after Angular initializes the component's views and child views.

interface AfterViewInit {
  ngAfterViewInit(): void
}  

The following snippet shows how a component can implement this interface to define its own view initialization method.

TypeScript
 
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements AfterViewInit {
  ngAfterViewInit() {
    // ...
  }
}

ngAfterViewChecked

This is called in response after Angular checks the component's views and child views.

interface AfterViewChecked {
  ngAfterViewChecked(): void
}

The following snippet shows how a component can implement this interface to define its own after-check functionality.

@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements AfterViewChecked {
  ngAfterViewChecked() {
    // ...
  }
}

ngOnDestroy

This is the cleanup phase just before Angular destroys the directive/component.

interface OnDestroy {
  ngOnDestroy(): void
}   

The following snippet shows how a component can implement this interface to define its own custom clean-up method.

TypeScript
 
@Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnDestroy {
  ngOnDestroy() {
    // ...
  }
}


AngularJS

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • After COVID, Developers Really Are the New Kingmakers
  • Autowiring in Spring
  • Challenges to Designing Data Pipelines at Scale
  • Why Is Software Integration Important for Business?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo