Component Lifecycle Flashcards

1
Q

What are key ideas behind Angular

A

Components, Templates, Directives, and Dependency Injection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Lifecycle Hook

A

An interface that allows you to tap into the lifecycle of directives and components as they are created, updated, and destroyed.

Each interface has a single hook method whose name is the interface name prefixed with ng.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the Lifecycle Hooks

A

ngOnChanges: When an input/output binding value changes.

ngOnInit: After the first ngOnChanges.

ngDoCheck: Developer’s custom change detection.

ngAfterContentInit: After component content initialized.

ngAfterContentChecked: After every check of component content.

ngAfterViewInit: After a component’s views are initialized.

ngAfterViewChecked: After every check of a component’s views.

ngOnDestroy: Just before the directive is destroyed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an NgModule

A

Ng Modules collect related code into functional sets; an Angular app is defined by a set of NgModules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a “view”

A

Components define views, which are sets of screen elements that Angular can choose among and modify according to your program logic and data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a “service”

A

Components use services, which provide specific functionality not directly related to views. Service providers can be injected into components as dependencies, making your code modular, reusable, and efficient.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

ngOnChanges()

A

A lifecycle hook
Respond when Angular sets or resets data-bound input properties. The method receives a SimpleChanges object of current and previous property values.

Note that this happens very frequently, so any operation you perform here impacts performance significantly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

ngOnInit()

A

A lifecycle hook
Initialize the directive or component after Angular first displays the data-bound properties and sets the directive or component’s input properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ngDoCheck()

A

A lifecycle hook

Detect and act upon changes that Angular can’t or won’t detect on its own.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ngAfterContentInit()

A

A lifecycle hook
Component Only
Respond after Angular projects external content into the component’s view, or into the view that a directive is in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

ngAfterContentChecked()

A

A lifecycle hook
Component Only
Respond after Angular checks the content projected into the directive or component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

ngAfterViewInit()

A

A lifecycle hook
Component Only
Respond after Angular initializes the component’s views and child views, or the view that contains the directive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ngAfterViewChecked()

A

A lifecycle hook
Component Only
Respond after Angular checks the component’s views and child views, or the view that contains the directive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

ngOnDestroy()

A

A lifecycle hook
Cleanup just before Angular destroys the directive or component. Unsubscribe Observables and detach event handlers to avoid memory leaks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When to use ngOnInit()

A
  • Perform complex initializations outside of the constructor. Components should be cheap and safe to construct.
    You should not, for example, fetch data in a component constructor.
  • Set up the component after Angular sets the input properties.
    Constructors should do no more than set the initial local variables to simple values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to use ngOnDestroy()

A

Put cleanup logic in ngOnDestroy(), the logic that must run before Angular destroys the directive.

This is the place to free resources that won’t be garbage-collected automatically. You risk memory leaks if you neglect to do so.

  • Unsubscribe from Observables and DOM events.
  • Stop interval timers.
  • Unregister all callbacks that the directive registered with global or application services.

Calling this method is also the time to notify another part of the app that this component is going away.