Angular is a TypeScript-based open-source web application framework developed and maintained by Google. It is used to build single-page applications (SPAs) — dynamic web apps that load a single HTML page and update it as the user interacts.
Angular Interview Questions
1. What is Angular?
2. What are the key features of Angular?
- Component-based architecture
- Two-way data binding
- Dependency Injection
- Directives and Pipes
- Routing and Lazy Loading
- RxJS for reactive programming
- Ahead-of-Time (AOT) Compilation
3. What is the difference between AngularJS and Angular (2+)?
| Feature | AngularJS | Angular (2+) |
|---|---|---|
| Language | JavaScript | TypeScript |
| Architecture | MVC | Component-based |
| Performance | Slower | Faster |
| Mobile support | Limited | Built-in support |
4. What is a decorator in Angular?
A decorator is a function that adds metadata to classes or properties.
- @Component – Declares a component
- @NgModule – Declares a module
- @Injectable – Declares a service
5. What is the difference between @Input() and @Output()?
@Input() – Passes data from parent to child
@Output() – Emits data from child to parent using EventEmitter
6. What are lifecycle hooks?
Hooks that allow you to act during different stages of a component:
- ngOnInit() – Called after component initialized
- ngOnChanges() – Called on input property changes
- ngOnDestroy() – Cleanup before component destroyed
- ngAfterViewInit() – After component’s view initialized
7. How does Angular handle dependency injection (DI)?
Angular uses an injector tree to provide services to components. Services are registered in providers (usually at the module level), and Angular automatically injects them using constructors.
constructor(private userService: UserService) {}
8. How does Angular routing work?
Angular uses RouterModule to map URL paths to components. Define routes in app-routing.module.ts.
const routes: Routes = [
{ path: 'home', component: HomeComponent },
];
9. What are route guards?
Guards control access to routes:
CanActivate – Before navigating to a route
CanDeactivate – Before leaving a route
Resolve – Preload data before route loads
10. What is lazy loading in Angular?
Lazy loading loads feature modules only when needed. Improves app performance.
{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
11. What is the difference between Template-driven and Reactive forms?
| Feature | Template-driven | Reactive |
|---|---|---|
| Approach | HTML-based | Code-driven (TypeScript) |
| Validation | Template (ngModel) | In component using FormBuilder |
| Suitable for | Simple forms | Complex/dynamic forms |
12. How do you add validation in Reactive forms?
this.form = this.fb.group({
name: ['', [Validators.required, Validators.minLength(3)]]
});
13. What is change detection in Angular?
Angular checks and updates the DOM when data changes using its change detection mechanism, usually triggered by events, observables, or zone.js.
14. Difference between ngOnChanges() and ngDoCheck()?
- ngOnChanges() – Triggered on @Input() changes
- ngDoCheck() – Custom change detection logic, runs more frequently
15. What is trackBy in *ngFor and why is it useful?
Used to optimize rendering in large lists.
trackById(index: number, item: any) {
return item.id;
}
16. What are pipes in Angular?
Pipes transform data in the template.
{{ birthday | date:'shortDate' }}
- Pure Pipe – Executes only on input change
- Impure Pipe – Executes on every change detection cycle
17. What is the difference between Subject and BehaviorSubject?
- Subject – Emits values to subscribers after subscription
- BehaviorSubject – Stores last emitted value and emits it immediately on subscription
18. How does Angular handle HTTP requests and errors?
Using HttpClient and RxJS:
this.http.get(url).pipe(
catchError(error => {
console.error('Error occurred:', error);
return throwError(() => error);
})
);
19. How do you optimize Angular apps for performance?
- Lazy loading
- OnPush change detection
- trackBy in *ngFor
- Code splitting & AOT compilation
- Avoiding memory leaks with unsubscribing
20. What is AOT vs JIT compilation?
| Feature | AOT (Ahead-of-Time) | JIT (Just-in-Time) |
|---|---|---|
| When compiled | Build time | Runtime (in browser) |
| Performance | Faster startup | Slower startup |
| Debugging | Harder | Easier |