Angular

 

1. What are Standalone Components in Angular? Why did Angular introduce them?

Answer

  • Standalone components remove the need for NgModule.

  • Simplifies app structure and bootstrapping.

  • Enables better tree-shaking and faster startup.

In Angular, a Signal is a new reactive state primitive (introduced in Angular 16) used to store and track reactive data. It lets Angular automatically update the UI when the value changes — without needing RxJS or async pipes for simple state. Think of a signal like a special variable that “signals” Angular whenever its value changes.

2. What are Angular Signals?

Answer

  • Signals are a reactive primitive that tracks state change without Observables.

  • Similar to React signals/solid-js.

Example

const counter = signal(0); counter.update(v => v + 1);

Why?

  • Predictable reactivity

  • No zone.js needed (future direction)

    Basic Example

    import { signal } from '@angular/core'; export class CounterComponent { count = signal(0); increment() { this.count.set(this.count() + 1); } }

    In HTML:

    <p>Count: {{ count() }}</p> <button (click)="increment()">Add</button>

    Notice we call count() to read the value.

Computed Signals

For derived values:

fullName = computed(() => `${this.firstName()} ${this.lastName()}`);

πŸ‘‚ Effect Signals

Runs automatically when signals change:

effect(() => { console.log("Count changed:", this.count()); });

SSR in Angular stands for Server-Side Rendering.
It means Angular renders the application on the server first (instead of only in the browser) and sends HTML to the client. After that, Angular runs normally in the browser.

How Angular SSR Works

Traditional SPA (no SSR):

  • Browser loads JS → JS builds HTML → User sees page

With SSR:

  1. Server generates HTML first

  2. Browser receives fully rendered HTML → fast display

  3. Angular bootstraps in browser and takes over (“hydration”)

πŸš€ How to enable SSR in Angular

Run:

ng add @angular/ssr

It adds server files and config.

Then start:

npm run dev:ssr

or build for production:

npm run build:ssr Hydration (in Angular and other web frameworks) is the process where the browser takes over a webpage that was pre-rendered on the server (SSR) and makes it fully interactive.

🧠 Why does Hydration exist?

With SSR (Server-Side Rendering):

  • Server sends ready-made HTML → page loads fast, good for SEO.

  • But HTML alone can’t respond to clicks, forms, navigation, etc.

Hydration connects that HTML to Angular's JavaScript, so the app becomes interactive without rebuilding the whole DOM.

npm run serve:ssr 🧠 Signals vs RxJS

SignalsRxJS
Simpler local component statePowerful async event streams
No subscriptions neededManual subscribe/unsubscribe
Great for UI data


Best for HTTP/websockets



11. What is the new Control Flow Syntax?

@if (loggedIn) { Welcome! } @else { Please login }

Replaces *ngIf*ngFor sugar for readability/performancel.


14. What is Zone-less Angular?

  • Angular future direction → optional zone.js

  • Signals trigger UI updates manually

  • Higher performance


πŸ”· 1. Standalone Components

Removes need for NgModules, simplifies app.

bootstrapApplication(AppComponent)

Benefits

  • Faster startup & tree-shaking

  • Less boilerplate

  • Better code-splitting


πŸ”· 2. Signals

Next-gen change detection (future Zone-less Angular).

const count = signal(0); count.update(v => v + 1);
SignalsObservables
StateStreams/events
Sync onlyAsync support
Simple UI stateEnterprise async flows

πŸ”· 3. New Angular Control Flow (@if, @for)

@if(user()) { ... } @else { ... } @for(item of list(); track $index) { ... }

Replaces *ngIf*ngFor with faster syntax.


πŸ”· 4. @defer – Deferred Views

Lazy renders UI sections based on triggers.

@defer (on idle) { <heavy-dashboard /> } @loading { Loading... }

πŸ”· 5. Functional Routing

export const routes = [ { path: 'home', loadComponent: () => import('./home.component') } ];

πŸ”· 6. Typed Reactive Forms

form = new FormGroup({ name: new FormControl<string>(''), });

Compile-time safety ✅


πŸ”· 7. SSR + Hydration

SEO + performance

ng add @angular/ssr

πŸ”· 8. Zones vs Zone-less

Zone-less + Signals = future Angular engine.

Benefits:

  • Faster rendering

  • More predictable reactivity


πŸ”· 9. RxJS Latest Patterns

Use:

  • switchMapcombineLatestcatchError

  • takeUntilDestroyed()

  • Replay/cache heavy API calls

Avoid manual subscriptions unless needed.




Comments

Popular posts from this blog

Archunit test

Hexagonal Architecture

visitor design pattern