Open reboottime opened 1 year ago
used only when you want to inject some services
ngOnChanges: This hook is called whenever one or more data-bound input properties of a directive or a component changes. It receives a SimpleChanges
object containing the previous and current values of the input properties.
When to use: Use this hook when you need to react to changes in input properties.
ngOnInit: This hook is called after Angular has initialized all data-bound properties of a directive. It is a good place to perform initialization logic for your component.
When to use: Use this hook when you need to set up the initial state of your component or perform one-time initialization tasks.
ngDoCheck: This hook is called during every change detection cycle. It allows you to perform your own custom change detection logic.
When to use: Use this hook when you need to implement custom change detection or perform more granular change detection.
ngAfterContentInit: This hook is called after Angular has projected external content (e.g., content passed between component tags).
When to use: Use this hook when you need to perform initialization logic that relies on content projection.
ngAfterContentChecked: This hook is called after Angular has checked the content projected into the component.
When to use: Use this hook when you need to perform additional checks or logic after Angular has checked the content.
ngAfterViewInit: This hook is called after Angular has initialized the component's views and child views.
When to use: Use this hook when you need to perform initialization logic that relies on the view or its child views.
ngAfterViewChecked: This hook is called after Angular has checked the component's views and child views.
When to use: Use this hook when you need to perform additional checks or logic after Angular has checked the views.
ngOnDestroy: This hook is called just before Angular destroys the component. It is a good place to clean up resources, unsubscribe from observables, etc.
When to use: Use this hook when you need to clean up resources or perform any necessary cleanup before a component is destroyed.
ngOnInit: Use this hook to fetch initial data from a server or set up initial state based on input properties.
ngOnChanges: Use this hook to react to changes in input properties. For example, if you have a component that displays user details, you can use this hook to update the display whenever the user data changes.
ngAfterViewInit: Use this hook when you need to interact with the DOM or perform operations that require the view to be fully initialized. For example, if you need to work with a specific element in the template, this is the appropriate hook.
ngOnDestroy: Use this hook to clean up resources like subscriptions to observables or event listeners to prevent memory leaks.
Remember, the choice of which lifecycle hook to use depends on the specific requirements of your application and component. Use the appropriate hooks to manage the state and behavior of your components effectively.
Angular directives are powerful features that allow you to extend HTML with custom attributes and elements. They are an essential part of building dynamic and interactive web applications using the Angular framework. Directives allow you to manipulate the Document Object Model (DOM), control the behavior of HTML elements, and create reusable components.
There are three types of directives in Angular:
Component Directives:
<app-header></app-header>
Attribute Directives: any valid html properties
<div [ngClass]="{'my-class': isActive}"></div>
Event binding Directives
<button type="button" (click)="clickMessage=$event" clickable>click with myClick</button>
{{clickMessage}}
<div *ngIf="condition">Content to display</div>
Here's a brief overview of some commonly used built-in directives in Angular:
Additionally, you can create your own custom directives when you need to encapsulate and reuse a specific behavior across your application.
To use a directive, you'll include it in your Angular application by importing it and then using it in your HTML templates.
For example, to use the ngIf directive:
import { Component } from '@angular/core';
@Component({
selector: 'app-example',
template: `
<div *ngIf="isVisible">Content to display</div>
`
})
export class ExampleComponent {
isVisible = true;
}
In this example, the *ngIf
directive is used to conditionally display the content based on the value of isVisible
.
Remember that directives are a fundamental part of Angular's declarative approach to building web applications, allowing you to create dynamic, data-driven user interfaces with ease.
Essentials of creating SPA using Angular Stack
This note is a essential list of creating angular app SPA, content covered are
--configuration
parameter, for example, build your code forstaging
environment.ng build --configuration=staging