Angular
47 questions across 2 experience levels
A Pipe is used to transform data in Angular templates before displaying it to the user. Pipes only change how data is displayed and do not modify the original value stored in the component.
{{ name | uppercase }}
{{ amount | currency:'INR' }}
{{ today | date:'dd/MM/yyyy' }}Think of a pipe as a display filter. The original data stays the same, but Angular shows it in a different format.
Angular provides several built-in pipes such as uppercase, lowercase, date, currency, percent, number, and slice.
{{ name | uppercase }}
{{ amount | currency:'INR' }}
{{ 0.75 | percent }}
{{ today | date:'dd/MM/yyyy' }}Instead of manually formatting data in TypeScript, Angular pipes format it directly in the template.
A Custom Pipe is a pipe created by developers when Angular's built-in pipes do not satisfy a business requirement. It helps keep formatting logic reusable and centralized.
@Pipe({name:'capitalize'})
export class CapitalizePipe implements PipeTransform {
transform(value:string){
return value.charAt(0).toUpperCase() + value.slice(1);
}
}If you need to format user names in many places, create one custom pipe and reuse it everywhere.
Custom Pipes improve reusability, keep templates cleaner, and separate formatting logic from business logic. Updating the pipe updates behavior everywhere it is used.
{{ product.price | customCurrency }}If currency formatting changes from Rupees to Dollars, update the pipe once instead of changing every component.
A Directive is a class that changes the behavior, structure, or appearance of DOM elements.
<div *ngIf='isLoggedIn'>Welcome</div>Directives tell Angular what to do with HTML elements.
Angular has three main types of directives: Component Directives, Structural Directives, and Attribute Directives.
Component -> @Component
Structural -> *ngIf, *ngFor
Attribute -> ngClass, ngStyleStructural directives change the DOM, while attribute directives modify existing elements.
Structural Directives add, remove, or manipulate DOM elements. Common examples are *ngIf, *ngFor, and *ngSwitch.
<div *ngIf='isAdmin'>Admin Panel</div>
<li *ngFor='let product of products'>{{product}}</li>Structural directives decide what appears on the page.
Attribute Directives modify the appearance or behavior of existing DOM elements without adding or removing them.
<div [ngClass]="{'active': isSelected}">Product</div>
<div [ngStyle]="{'color':'red'}">Error</div>Attribute directives change how an element looks or behaves.
ngIf removes or adds elements to the DOM, while hidden only hides the element but keeps it in the DOM.
<div *ngIf='showContent'>Content</div>
<div [hidden]='!showContent'>Content</div>ngIf removes the chair from the room. hidden covers the chair with a cloth.
Pipes transform data for display. Directives change the DOM's structure, appearance, or behavior.
{{ name | uppercase }}
<div *ngIf='isLoggedIn'>Welcome</div>Pipe changes the data. Directive changes the HTML.
A Service is a reusable class that contains business logic, API calls, shared state, or utility methods. Services help keep components clean and promote code reusability.
@Injectable({providedIn:'root'})
export class UserService {
getUsers() {
return this.http.get('/api/users');
}
}Instead of writing API calls in every component, create them once in a service and reuse them everywhere.
Services help centralize business logic, improve code reusability, reduce duplicate code, and keep components focused on UI logic.
constructor(private userService: UserService) {}Think of a service as a common helper that multiple components can use.
Dependency Injection is a design pattern where Angular automatically creates and provides required dependencies to a class instead of the class creating them manually.
constructor(private productService: ProductService) {}Instead of cooking your own food, a waiter brings it to your table. Angular provides the service instead of the component creating it.
A dependency is any service or object that a class needs to function.
constructor(private userService: UserService) {}If ProductComponent needs ProductService, then ProductService is its dependency.
Angular creates a single shared instance of the service for the entire application. This is known as a Singleton Service.
@Injectable({providedIn:'root'})One service instance shared across all components.
HttpClient is Angular's built-in service used to communicate with backend APIs. It supports GET, POST, PUT, PATCH, and DELETE requests.
this.http.get('/api/users')HttpClient is Angular's way of talking to backend APIs.
GET is used to retrieve data, while POST is used to create or submit data to the server.
this.http.get('/api/products')
this.http.post('/api/users', payload)GET = Fetch Data, POST = Save Data.
PUT replaces the entire resource, while PATCH updates only specific fields.
this.http.put('/api/user/1', fullUser)
this.http.patch('/api/user/1', {name:'Manav'})PUT updates everything. PATCH updates only what changed.
Lifecycle Hooks are methods that Angular calls during different stages of a component's life, from creation to destruction.
ngOnInit() {}
ngOnDestroy() {}Angular notifies you when a component starts, updates, and ends.
ngOnInit runs after Angular initializes the component. It is commonly used for API calls and initialization logic.
ngOnInit() {
this.loadProducts();
}Component is ready, now load data.
ngOnDestroy runs before a component is removed from the DOM. It is commonly used to clean up subscriptions, intervals, and event listeners.
ngOnDestroy() {
this.subscription.unsubscribe();
}Clean up before leaving the component.
ngAfterViewInit runs after Angular has fully initialized the component view and child views.
@ViewChild('input') input!: ElementRef;
ngAfterViewInit() {
this.input.nativeElement.focus();
}The HTML is ready, now safely access DOM elements.
Unsubscribing prevents memory leaks and unnecessary processing when a component is destroyed.
ngOnDestroy() {
this.subscription.unsubscribe();
}Stop listening when you no longer need updates.
Angular Routing allows navigation between different components without reloading the entire page. It enables Single Page Application (SPA) behavior.
Like switching pages inside the same app without full page reload.
routerLink is used to navigate between routes from HTML templates without reloading the page.
<a routerLink='/products'>Products</a>It is Angular's way of navigating like an anchor tag.
Angular updates the URL, matches it with route configuration, and loads the corresponding component inside router-outlet without reloading the page.
Click link โ URL changes โ Angular loads matching component.
router-outlet is a placeholder where Angular renders the matched component based on the current route.
It acts as a container where routed components are displayed.
Angular router checks the current URL on app startup, matches it with route configuration, and loads the corresponding component.
Direct URL like /products still loads correct component without clicking anything.
Route parameters are dynamic values in the URL used to pass data like IDs.
/product/:id โ /product/101Used when value is required in route.
Route params are part of URL path and required, while query params are optional values passed after ? used for filters or pagination.
Route param: /product/1 | Query param: /product?page=1
Child routes allow nesting routes inside a parent route and render them inside a child router-outlet.
Used for layouts like Admin โ Users, Settings, Reports.
Child components will not render because Angular has no placeholder to display them.
Route matches but nothing is displayed.
Lazy Loading loads modules only when required instead of loading everything at application startup, improving performance.
Modules load only when user navigates to that section.
A lazy-loaded module is loaded only when the user navigates to its route.
Module downloads only when route is visited.
CanActivate is a route guard used to allow or block access to a route based on conditions like authentication.
Prevents unauthorized users from accessing routes.
Navigation is cancelled and the component is not created. User stays on current page unless manually redirected.
Route access is blocked.