Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 77 ยท Web and Frontend Interview Questions

Angular and Vue: Components, Reactivity and Change Detection

Prepare Angular and Vue interviews: components, Vue reactivity, Angular change detection and signals, RxJS and lifecycle hooks, with runnable stand-ins.

Angular and Vue answer the same question, "how does the screen stay in sync with the data?", in different ways. Vue tracks exactly which data each piece of the template reads and updates only that piece. Angular, historically, checked the component tree for changes after every browser event, and now offers signals that work more like Vue. Interviews probe exactly this difference, then branch into components, services, forms and the RxJS library.

Neither framework can run in the site's code runner (Angular needs a compiler and build step; Vue's template compiler is separate), so the real framework code in this lesson is read-only. To make the key ideas runnable, the examples are small stand-ins in plain JavaScript that reproduce the mechanism: a Proxy-based reactivity system like Vue's, signals and change detection like Angular's, and a minimal Observable. For the full lessons, see the Angular and Vue.js tutorials, and for the shared foundation, React Interview Questions and JavaScript scope and closures.

Two ways to know what changed

Vue Tracks which data each render reads.
Angular (classic) Re-checks the component tree after events.
Angular signals Tracks reads, like Vue.

The newer styles converge on fine-grained updates.

1 / 2

How do components work in Angular and Vue?

Short answer: Both build the UI from components: a template, some state and logic, and an interface of inputs (data coming in) and outputs or events (notifications going out). The data-flow rule is the same in both: props down, events up. A child never edits its parent's data; it emits an event and the parent decides.

TypeScript
// Read-only: an Angular standalone component
@Component({
  selector: 'app-counter',
  template: `
    <button (click)="increment()">Clicked {{ count() }} times</button>
    @if (count() > 3) { <p>That is a lot of clicks.</p> }
  `,
})
export class CounterComponent {
  count = signal(0);
  increment() { this.count.update(n => n + 1); }
}
VUE
<!-- Read-only: the same component in Vue 3 with <script setup> -->
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>

<template>
  <button @click="count++">Clicked {{ count }} times</button>
  <p v-if="count > 3">That is a lot of clicks.</p>
</template>

Angular uses TypeScript classes, decorators, dependency injection and a full framework (router, forms, HTTP client). Vue is a progressive library with an official router and the Pinia store. In Vue, ref holds a primitive and needs .value in script code (the template unwraps it); reactive makes an object deeply reactive.

How does Vue's reactivity work?

Short answer: Vue 3 wraps your state in a JavaScript Proxy. When a render function or computed reads a property, the proxy tracks that dependency; when the property is written, the proxy triggers every effect that depends on it. So the template re-runs only for the data it actually used. Vue 2 used Object.defineProperty, which could not notice added or deleted properties, which is why Vue.set existed.

The stand-in below implements reactive, effect and computed in about 40 lines. Read it as a model of the real system: change quantity and the total recomputes, and writing the same value again does nothing.

JS Reactivity from scratch with a Proxy

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Other Vue questions you should be able to answer in two sentences:

  • computed versus watch: computed derives a cached value from other state and is lazy; watch runs a side effect (a request, a log) when something changes.
  • v-if versus v-show: v-if creates and destroys the element; v-show keeps it and toggles display: none. Use v-show for frequent toggles and v-if when the content is expensive or rarely shown.
  • :key in v-for: a stable unique key lets the diffing algorithm reuse and reorder elements correctly. Do not use the array index if items can be reordered or removed.
  • Lifecycle: onMounted, onUpdated, onUnmounted (Options API: mounted, updated, unmounted). Clean up timers and listeners in onUnmounted.
  • Pinia versus props: use props and events for parent-child data; use a store for state shared by distant components.

What is change detection in Angular?

Short answer: Change detection is how Angular decides which templates to re-render. Classically, Zone.js patches browser async APIs (events, timers, promises) so Angular knows when something might have changed, and then it walks the component tree from the root, comparing each template binding to its previous value. The default strategy checks every component every time. The OnPush strategy checks a component only when one of its @Input references changes (a new object, not a mutated one), when an event fires inside it, when it is marked dirty, or when an observable bound with the async pipe emits. Recent versions can also run zoneless, driven by signals.

The consequence interviewers listen for: with OnPush, mutating an input object (user.name = 'x') is not noticed; you must pass a new object (immutable updates).

Signals make this finer-grained. A signal holds a value; reading it inside a template or a computed records the dependency; writing it notifies exactly the readers. effect runs side effects when signals it reads change. The stand-in below shows both ideas: a signal with an effect, then a count of how many times each component is checked in a tree under Default and OnPush.

JS Signals and Default versus OnPush checks

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The tree under Default is checked 11 times in every component. Under OnPush the List is checked only on the first render and when its input reference changed (twice), and Row only once. (In real Angular the details are richer, for example an event inside the component also marks it dirty, but the principle is the same.)

What are services, dependency injection and RxJS in Angular?

A service is a class holding logic or shared state; dependency injection supplies it. @Injectable({ providedIn: 'root' }) creates one app-wide instance, and a component gets it with inject(MyService) or a constructor parameter. Providing a service at component level gives each component its own instance. This is the same idea as IoC in Spring.

RxJS gives Angular its asynchronous model. An Observable is a lazy stream of values over time: nothing happens until you subscribe, and each subscription runs the producer again (a cold observable, like HttpClient calls). Operators such as map, filter, switchMap transform streams. The leak to avoid: a manual subscription that is never cancelled keeps running after the component is destroyed. Prefer the async pipe in templates (it unsubscribes for you), takeUntilDestroyed, or signals converted with toSignal.

JS A minimal cold Observable

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Quick answers for the follow-ups

  • Angular lifecycle: ngOnChanges (inputs changed), ngOnInit (once, after the first inputs), ngAfterViewInit, ngOnDestroy (clean up).
  • @for needs track: like Vue's :key, it tells Angular how to match items between renders.
  • Reactive versus template-driven forms (Angular): reactive forms define the model in code, are easier to test and scale; template-driven forms use directives and suit small forms.
  • ngIf equivalent: @if (new control flow) replaces the *ngIf directive and is built in.
  • Vue nextTick: Vue batches DOM updates; await nextTick() waits until the DOM reflects your state change.
  • When to pick which: Angular for large teams that want a complete, opinionated framework; Vue for a gentle learning curve and flexibility.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How does Vue 3 know which parts of the UI to update when data changes?

    Reading a property inside an effect registers a dependency; writing it triggers the dependent effects. That is fine-grained reactivity.
  2. 2. In an OnPush Angular component, the parent mutates a property of the object it passes as an input. What happens?

    OnPush compares input references. Mutation keeps the same reference, so use immutable updates.
  3. 3. What is the difference between v-if and v-show in Vue?

    v-if creates and destroys DOM nodes (cheap to hide, costlier to toggle); v-show keeps the node and toggles CSS display.
  4. 4. Why is a cold Observable described as lazy?

    A cold observable does no work without a subscriber, and every subscriber triggers its own execution, such as a separate HTTP request.
  5. 5. Which is the safest way to avoid an RxJS subscription leak in an Angular template?

    The async pipe subscribes when the view is created and unsubscribes when it is destroyed.

Summary

  • Both frameworks use components with props (inputs) down and events (outputs) up.
  • Vue 3 reactivity: a Proxy tracks reads and triggers dependent effects on writes; computed derives cached values, watch runs side effects.
  • v-if creates and destroys; v-show toggles display; always give v-for a stable key.
  • Angular change detection: Zone.js plus a tree walk (Default), or OnPush and signals for fine-grained updates; OnPush needs new object references.
  • Angular services and DI mirror IoC containers; RxJS observables are lazy and cold, and leaks are avoided with the async pipe or takeUntilDestroyed.

Next: Android and iOS Interview Questions.