Angular

Angular interview questions

Prepare for your Angular interview with these common questions and answers.

01What is Angular?

Angular is a TypeScript-based, open-source web application framework led by the Angular Team at Google. It's a complete rewrite of AngularJS and is used for building single-page client applications using HTML and TypeScript.

02What are directives in Angular?

Directives are classes that add additional behavior to elements in your Angular applications. There are three kinds of directives:

  • Components: Directives with a template.
  • Structural directives: Change the DOM layout by adding and removing DOM elements (e.g., *ngIf, *ngFor).
  • Attribute directives: Change the appearance or behavior of an element, component, or another directive (e.g., ngClass, ngStyle).
03What is dependency injection in Angular?

Dependency Injection (DI) is a core concept and design pattern in Angular. It's a way to provide components with the services or objects they need (their dependencies) instead of having them create the dependencies themselves. Angular's DI framework provides dependencies to a class upon instantiation.

04What is the difference between a component and a directive?

A component is a directive with a template. It's a specific type of directive. A component is used to define a view with its logic. A directive is used to add behavior to an existing DOM element. Essentially, every component is a directive, but not every directive is a component.

05What are the building blocks of an Angular application?

The main building blocks are:

  • Modules (@NgModule): Organize an application into cohesive blocks of functionality.
  • Components (@Component): Control a patch of screen called a view.
  • Templates: The HTML view of a component.
  • Services: Reusable classes for logic that isn't tied to a specific view.
  • Directives (@Directive): Add behavior to DOM elements.
  • Pipes (@Pipe): Transform data for display.
06What are Angular lifecycle hooks?

Lifecycle hooks are methods that Angular calls on directives and components as it creates, changes, and destroys them. Examples include:

  • ngOnChanges: Called when an input binding value changes.
  • ngOnInit: Called after the first ngOnChanges.
  • ngDoCheck: Called during every change detection run.
  • ngAfterViewInit: Called after the component's view has been initialized.
  • ngOnDestroy: Called just before the directive is destroyed.
07What is data binding in Angular?

Data binding is a mechanism for coordinating parts of a template with parts of a component. Angular supports both one-way and two-way data binding.

  • One-way: From component to view ({{value}}, [property]) or from view to component ((event)).
  • Two-way: [(ngModel)] combines property binding and event binding.
08What is the difference between `constructor` and `ngOnInit`?

The constructor is a default method of the class that is executed when the class is instantiated. It's used for dependency injection.

ngOnInit is a lifecycle hook called by Angular to indicate that the component is initialized. It's the right place to put initialization logic, like fetching data, as the component's bindings are available.

09What are services in Angular?

Services are singleton objects that get instantiated only once during the lifetime of an application. They are used to organize and share business logic, models, or data and functions with different components of an Angular application. They are a great way to share information among classes that don't know each other.

10What is RxJS and Observables?

RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code.

An Observable is a unique object that can emit a stream of data over time. You can subscribe to an observable to receive notifications when new data is emitted.

11What is the difference between an Observable and a Promise?

Promise: Handles a single event when an async operation completes or fails. It is not cancellable.

Observable: Can handle multiple values over time. It is cancellable. Observables provide operators like map, filter, reduce, etc., which Promises do not have.

12What is Angular CLI?

The Angular CLI (Command Line Interface) is a tool to initialize, develop, scaffold, and maintain Angular applications directly from a command shell. It helps in creating projects, components, services, etc., and handles building, testing, and deployment.

13What is routing in Angular?

Routing allows you to navigate from one view to the next as users perform application tasks. The Angular Router enables navigation by interpreting a browser URL as an instruction to change the view. It's configured in a module like AppRoutingModule.

14What are pipes?

Pipes are simple functions that accept an input value and return a transformed value. They are used in templates to transform data for display. Angular has built-in pipes like DatePipe, UpperCasePipe, and CurrencyPipe. You can also create custom pipes.

15What is change detection and how does it work?

Change detection is the mechanism by which Angular synchronizes the state of the application with the view. It checks when a component's data changes, and then re-renders the view to reflect that change. Angular uses Zones (Zone.js) to detect when an async operation has finished, which triggers change detection.

16What are decorators in Angular?

Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In Angular, decorators are functions that modify JavaScript classes. Examples: @Component, @NgModule, @Injectable.

17What is AOT (Ahead-of-Time) compilation?

AOT compilation is a process where the Angular compiler converts your Angular HTML and TypeScript code into efficient JavaScript code during the build phase, before the browser downloads and runs that code. This results in faster rendering and a smaller application size compared to JIT (Just-in-Time) compilation.

18What is lazy loading?

Lazy loading is a design pattern for initializing modules or components on demand. In Angular, it allows you to load feature modules only when their routes are activated. This helps to keep initial bundle sizes smaller, which can decrease application load time.

19What are Angular forms? (Template-driven vs. Reactive)

Angular provides two approaches to handling user input through forms:

  • Template-driven forms: Logic is placed in the template. They are simple and good for basic forms. They use directives like ngModel.
  • Reactive forms: Logic is defined in the component class. They are more scalable, reusable, and testable. They use FormControl, FormGroup, and FormBuilder.
20What is the purpose of `async` pipe?

The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. When a new value is emitted, the async pipe marks the component to be checked for changes. When the component gets destroyed, the async pipe unsubscribes automatically to avoid potential memory leaks.

21What is TypeScript?

TypeScript is a superset of JavaScript that adds static types. Angular is written in TypeScript. Using TypeScript provides benefits like better tooling, cleaner code, and the ability to catch errors during development.

22What is the `RouterModule.forRoot()` vs `RouterModule.forChild()`?

forRoot() is a static method that creates a module that contains all the directives, the given routes, and the router service itself. It should only be used in the root application module (AppModule).

forChild() is a static method that creates a module that contains all the directives and the given routes, but does not include the router service. It should be used in feature modules.

23What is content projection?

Content projection is a way to pass content from a parent component into a child component's template. This is achieved using the <ng-content> element in the child component's template. It's similar to slots in Vue or props.children in React.

24What are interceptors?

Interceptors are a feature of Angular's HttpClient. They allow you to intercept incoming or outgoing HTTP requests and responses. This is useful for tasks like adding authentication headers to requests or logging responses.

25What is a pure pipe vs. an impure pipe?

A pure pipe is only executed when Angular detects a pure change to the input value (a change to a primitive input value or a changed object reference). This is the default.

An impure pipe is executed during every component change detection cycle, regardless of whether the value or parameters have changed.

26What is the difference between `@ViewChild` and `@ContentChild`?

@ViewChild is used to get a reference to a DOM element or a component that is in the component's own template.

@ContentChild is used to get a reference to a DOM element or a component that has been projected into the component using <ng-content>.

27What is NgRx?

NgRx is a framework for building reactive applications in Angular. It provides state management, isolation of side effects, entity collection management, and router bindings. It's inspired by Redux and is used for managing complex state in large applications.

28How do you share data between components?

Several ways:

  • Parent to Child: Using @Input() decorator.
  • Child to Parent: Using @Output() decorator and EventEmitter.
  • Unrelated Components: Using a shared service with a Subject or BehaviorSubject.
29What is the `trackBy` function in `*ngFor`?

trackBy is a function that you can provide to *ngFor to help Angular track items in a list. When the list changes, Angular can re-render only the changed items instead of the entire list. This can provide a significant performance boost for large lists.

30What are standalone components?

Standalone components, introduced in Angular 14, are components that don't need to be declared in an NgModule. They can directly manage their own template dependencies. This simplifies the authoring experience by reducing the need for NgModules.

Next step

Preparing for a role?

Tell us what you’re aiming for. A mentor can help you close the gaps before the interview.