{jsonbody.com}

Top 40 Angular Interview Questions & Answers

Top 40 Angular interview questions and answers for freshers and experienced developers: components, DI, RxJS, signals, change detection and routing.

Loading…

Angular Fundamentals

Q1. What is Angular?

A TypeScript-based front-end framework from Google for building single-page applications. It includes routing, forms, HTTP, dependency injection and testing tools out of the box.

Q2. What is the difference between AngularJS and Angular?

AngularJS (version 1) is the older JavaScript framework built on scopes and controllers. Angular (version 2 and later) is a full rewrite in TypeScript, based on components and a faster rendering engine.

Q3. What is a component?

A class with the @Component decorator that controls part of the screen. It has a template, styles and a selector used to place it in other templates.

Q4. What are standalone components?

Components declared with standalone: true (the default since Angular 19) that import their own dependencies directly, without needing an NgModule.

Q5. What is an NgModule?

A class with @NgModule that groups components, directives and pipes and configures providers. Newer apps rely mainly on standalone components instead.

Q6. What are directives?

Classes that add behaviour to elements. Components are directives with templates, attribute directives change appearance or behaviour, and structural directives change the DOM layout.

Q7. What are pipes?

Functions used in templates to transform displayed values, such as {{ price | currency }} or {{ date | date:'short' }}. You can write custom pipes with @Pipe.

Q8. What is the difference between pure and impure pipes?

A pure pipe runs only when its input reference changes, so it is fast. An impure pipe runs on every change detection cycle, which can hurt performance.

Q9. What is the Angular CLI?

The command-line tool to create projects and generate components, services and more with ng generate. It also runs the dev server, tests and production builds.

Q10. What is the new control flow syntax?

Built-in template blocks such as @if, @for and @switch, which replace *ngIf, *ngFor and ngSwitch. @for requires a track expression, which improves list performance.

Data Binding, Components & Lifecycle

Q11. What types of data binding does Angular support?

Interpolation {{ value }}, property binding [src]="url", event binding (click)="save()" and two-way binding [(ngModel)]="name".

Q12. How do parent and child components communicate?

The parent passes data down with @Input() or the input() function, and the child sends events up with @Output() and an EventEmitter, or the output() function. Shared services also work for distant components.

Q13. What are the main lifecycle hooks?

ngOnInit runs once after inputs are set, ngOnChanges when inputs change, ngAfterViewInit after the view renders, and ngOnDestroy before removal, which is where you clean up subscriptions.

Q14. What is the difference between the constructor and ngOnInit?

The constructor is for dependency injection and simple setup. ngOnInit runs after Angular has set the inputs, so initialisation logic that needs them belongs there.

Q15. What is content projection?

Passing markup from a parent into a child's template with <ng-content>. Named slots use <ng-content select="...">.

Q16. What are @ViewChild and @ContentChild?

@ViewChild gives a reference to an element or component in the component's own template. @ContentChild gives a reference to projected content. Both are available from ngAfterViewInit and ngAfterContentInit.

Q17. What is ng-template?

A template that is not rendered until used, for example by a structural directive or ngTemplateOutlet. It enables reusable fragments and custom structural directives.

Q18. What is view encapsulation?

How component styles are scoped. The default Emulated mode adds unique attributes so styles affect only that component. ShadowDom uses native Shadow DOM, and None makes styles global.

Q19. What are signals in Angular?

Reactive values created with signal(), read by calling them and changed with set or update. computed() derives values and effect() runs side effects, and Angular updates only what depends on them.

Q20. What is the difference between signals and RxJS observables?

Signals hold a current value and suit synchronous UI state. Observables model streams of events over time and suit HTTP, WebSockets and complex async flows. toSignal and toObservable convert between them.

Services, Dependency Injection & RxJS

Q21. What is a service in Angular?

A class with @Injectable that holds logic or data shared across components, such as API calls or state. Angular creates it and injects it where it is needed.

Q22. How does dependency injection work in Angular?

Classes declare what they need in the constructor or with inject(), and Angular's injector supplies instances from providers. This makes code loosely coupled and easy to test.

Q23. What does providedIn: 'root' do?

It registers a service as a single app-wide instance that is tree-shakable, so it is left out of the bundle if nothing uses it.

Q24. What is the hierarchical injector?

Injectors form a tree of environment injectors and element injectors. A service provided in a component is a new instance for that component and its children.

Q25. What is RxJS?

A library for reactive programming with observables. Angular uses it for HTTP, router events and forms, and its operators transform, combine and filter streams.

Q26. What is the difference between switchMap, mergeMap, concatMap and exhaustMap?

switchMap cancels the previous inner observable, which suits search. mergeMap runs all in parallel. concatMap runs them in order. exhaustMap ignores new values while one is running, which suits submit buttons.

Q27. What is the difference between a Subject and a BehaviorSubject?

A Subject emits only to subscribers present at the time. A BehaviorSubject needs an initial value and gives the latest value to every new subscriber.

Q28. How do you avoid memory leaks from subscriptions?

Use the async pipe in templates, takeUntilDestroyed() in components, or unsubscribe in ngOnDestroy. HTTP observables complete on their own.

Q29. What does HttpClient return?

Cold observables that send the request only when subscribed. Interceptors can add headers such as auth tokens, handle errors and log requests in one place.

Q30. What is an HTTP interceptor?

A function or class that sees every HttpClient request and response. It is used to add tokens, retry or handle errors globally, and show loading indicators.

Routing, Forms & Performance

Q31. How does routing work in Angular?

You define routes that map paths to components, and <router-outlet> displays the active one. routerLink navigates in templates and Router.navigate in code.

Q32. What is lazy loading in Angular?

Loading a route's code only when the user visits it, with loadComponent or loadChildren. It shrinks the initial bundle and speeds up the first load.

Q33. What are route guards?

Functions such as canActivate, canMatch and canDeactivate that allow or block navigation, for example to protect pages that need login or warn about unsaved changes.

Q34. What is a resolver?

A function that fetches data before a route activates, so the component receives it ready instead of loading after rendering.

Q35. What is the difference between template-driven and reactive forms?

Template-driven forms are defined in the template with ngModel and suit simple forms. Reactive forms are built in code with FormGroup and FormControl, which makes them easier to test, validate and change dynamically.

Q36. How do you write a custom validator?

Write a function that takes an AbstractControl and returns null when valid or an error object such as { weakPassword: true } when invalid, then add it to the control.

Q37. How does change detection work in Angular?

Angular checks component bindings after events, timers and HTTP responses, historically triggered by Zone.js. Newer apps can run zoneless, with signals telling Angular exactly what changed.

Q38. What does ChangeDetectionStrategy.OnPush do?

It checks a component only when an input reference changes, an event fires inside it, an async pipe emits or a signal it reads changes. It greatly reduces work in large apps.

Q39. What is AOT compilation?

Ahead-of-Time compilation turns templates into JavaScript at build time. It gives faster rendering, smaller bundles and catches template errors before the app runs.

Q40. How do you improve Angular performance?

Use OnPush or signals, lazy-load routes, use track in @for loops, defer heavy parts with @defer, and avoid calling functions in templates.