Frontend Framework · Arena

Angular

104 challenges · 0 mastered

0%
Arena cleared
0/104 mastered Let's go 🚀
Answer

A Directive is a class that changes the behavior, structure, or appearance of DOM elements.

Code Example
<div *ngIf='isLoggedIn'>Welcome</div>
💡 Simple Analogy

Directives tell Angular what to do with HTML elements.

Answer

Angular has three main types of directives: Component Directives, Structural Directives, and Attribute Directives.

Code Example
Component -> @Component
Structural -> *ngIf, *ngFor
Attribute -> ngClass, ngStyle
💡 Simple Analogy

Structural directives change the DOM, while attribute directives modify existing elements.

Answer

Structural Directives add, remove, or manipulate DOM elements. Common examples are *ngIf, *ngFor, and *ngSwitch.

Code Example
<div *ngIf='isAdmin'>Admin Panel</div>
<li *ngFor='let product of products'>{{product}}</li>
💡 Simple Analogy

Structural directives decide what appears on the page.

Answer

Attribute Directives modify the appearance or behavior of existing DOM elements without adding or removing them.

Code Example
<div [ngClass]="{'active': isSelected}">Product</div>
<div [ngStyle]="{'color':'red'}">Error</div>
💡 Simple Analogy

Attribute directives change how an element looks or behaves.

Answer

ngIf removes or adds elements to the DOM, while hidden only hides the element but keeps it in the DOM.

Code Example
<div *ngIf='showContent'>Content</div>
<div [hidden]='!showContent'>Content</div>
💡 Simple Analogy

ngIf removes the chair from the room. hidden covers the chair with a cloth.

Answer

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.

Code Example
{{ name | uppercase }}
{{ amount | currency:'INR' }}
{{ today | date:'dd/MM/yyyy' }}
💡 Simple Analogy

Think of a pipe as a display filter. The original data stays the same, but Angular shows it in a different format.

Answer

Angular provides several built-in pipes such as uppercase, lowercase, date, currency, percent, number, and slice.

Code Example
{{ name | uppercase }}
{{ amount | currency:'INR' }}
{{ 0.75 | percent }}
{{ today | date:'dd/MM/yyyy' }}
💡 Simple Analogy

Instead of manually formatting data in TypeScript, Angular pipes format it directly in the template.

Answer

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.

Code Example
@Pipe({name:'capitalize'})
export class CapitalizePipe implements PipeTransform {
  transform(value:string){
    return value.charAt(0).toUpperCase() + value.slice(1);
  }
}
💡 Simple Analogy

If you need to format user names in many places, create one custom pipe and reuse it everywhere.

Answer

Custom Pipes improve reusability, keep templates cleaner, and separate formatting logic from business logic. Updating the pipe updates behavior everywhere it is used.

Code Example
{{ product.price | customCurrency }}
💡 Simple Analogy

If currency formatting changes from Rupees to Dollars, update the pipe once instead of changing every component.

Answer

Pipes transform data for display. Directives change the DOM's structure, appearance, or behavior.

Code Example
{{ name | uppercase }}
<div *ngIf='isLoggedIn'>Welcome</div>
💡 Simple Analogy

Pipe changes the data. Directive changes the HTML.

Answer

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.

Code Example
@Injectable({providedIn:'root'})
export class UserService {
 getUsers() {
  return this.http.get('/api/users');
 }
}
💡 Simple Analogy

Instead of writing API calls in every component, create them once in a service and reuse them everywhere.

Answer

Services help centralize business logic, improve code reusability, reduce duplicate code, and keep components focused on UI logic.

Code Example
constructor(private userService: UserService) {}
💡 Simple Analogy

Think of a service as a common helper that multiple components can use.

Answer

Dependency Injection is a design pattern where Angular automatically creates and provides required dependencies to a class instead of the class creating them manually.

Code Example
constructor(private productService: ProductService) {}
💡 Simple Analogy

Instead of cooking your own food, a waiter brings it to your table. Angular provides the service instead of the component creating it.

Answer

A dependency is any service or object that a class needs to function.

Code Example
constructor(private userService: UserService) {}
💡 Simple Analogy

If ProductComponent needs ProductService, then ProductService is its dependency.

Answer

Angular creates a single shared instance of the service for the entire application. This is known as a Singleton Service.

Code Example
@Injectable({providedIn:'root'})
💡 Simple Analogy

One service instance shared across all components.

Answer

@Input is used for parent-to-child communication. It allows a parent component to pass data to a child component.

Code Example
// Parent
<app-user [userName]='userName'></app-user>

// Child
@Input() userName: string = '';
💡 Simple Analogy

Parent passes data down to child like a parent handing a child their lunchbox.

Answer

@Output is used for child-to-parent communication. It allows a child component to notify or send data to its parent component using EventEmitter.

Code Example
// Child
@Output() saved = new EventEmitter<string>();

// Parent
<app-form (saved)='onSave($event)'></app-form>
💡 Simple Analogy

A child component emits a save event when a button is clicked.

Answer

EventEmitter is used with @Output to emit events or data from a child component to its parent component.

Code Example
@Output() saveClicked = new EventEmitter<UserData>();

onSave() {
  this.saveClicked.emit(this.userData);
}
💡 Simple Analogy

saveClicked.emit(userData) fires the event up to the parent.

Answer

A shared service can be used to store and share data between unrelated components.

Code Example
@Injectable({providedIn:'root'})
export class SharedDataService {
  user$ = new BehaviorSubject<User|null>(null);
}
💡 Simple Analogy

NavbarComponent and ProfileComponent can access the same UserService.

Answer

Lifecycle Hooks are methods that Angular calls during different stages of a component's life, from creation to destruction.

Code Example
ngOnInit() {}
ngOnChanges() {}
ngOnDestroy() {}
💡 Simple Analogy

Angular notifies you when a component starts, updates, and ends.

Answer

ngOnInit runs after Angular initializes the component. It is commonly used for API calls and initialization logic.

Code Example
ngOnInit() {
  this.loadProducts();
}
💡 Simple Analogy

Component is ready, now load data.

Answer

ngOnDestroy runs before a component is removed from the DOM. It is commonly used to clean up subscriptions, intervals, and event listeners.

Code Example
ngOnDestroy() {
  this.subscription.unsubscribe();
}
💡 Simple Analogy

Clean up before leaving the component.

Answer

ngAfterViewInit runs after Angular has fully initialized the component view and child views.

Code Example
@ViewChild('input') input!: ElementRef;
ngAfterViewInit() {
  this.input.nativeElement.focus();
}
💡 Simple Analogy

The HTML is ready, now safely access DOM elements.

Answer

Unsubscribing prevents memory leaks and unnecessary processing when a component is destroyed.

Code Example
ngOnDestroy() {
  this.subscription.unsubscribe();
}
💡 Simple Analogy

Stop listening when you no longer need updates.