Angular Fundamentals Flashcards
These questions cover the fundamental concepts and technical aspects typically asked during Angular interviews.
What is Angular?
Angular is a platform and framework for building single-page client applications using HTML and TypeScript.
It provides a set of tools and libraries for routing, forms, HTTP client communication, and more, making it an all-in-one solution for building robust web applications.
Explain the concept of components in Angular.
Components are the building blocks of an Angular application.
A component is a TypeScript class with a decorator that defines a view (HTML template), style (CSS or SCSS), and business logic.
Every Angular application has at least one root component, and components can be nested within each other to create a tree-like structure.
What is the difference between Angular and AngularJS?
Architecture: Angular uses a component-based architecture, while AngularJS is MVC-based.
Language: Angular uses TypeScript, while AngularJS is based on JavaScript.
Performance: Angular has better performance due to features like AOT (Ahead-of-Time) compilation and a better change detection mechanism.
Mobile Support: Angular is designed with mobile support in mind, whereas AngularJS is not.
What are services in Angular?
Services are used to create reusable business logic that can be shared across multiple components.
They are typically used for:
- data retrieval,
- handling HTTP requests,
- performing calculations.
Services are injected into components using Angular’s dependency injection (DI) system.
What is dependency injection (DI) in Angular?
Dependency Injection is a design pattern in which a class asks for dependencies from external sources rather than creating them itself.
Angular’s DI framework allows developers to define and inject dependencies like services into components, making the code modular and easier to test.
What is Angular CLI, and what are some of its main commands?
Angular CLI (Command Line Interface) is a tool that helps automate the development workflow in Angular projects. It offers commands like:
-
ng new
: Create a new Angular project. -
ng serve
: Run the application in development mode. -
ng generate component
<component-name>: Generate a new component.</component-name> -
ng build
: Build the application for deployment. -
ng test
: Run unit tests for the project.
What is change detection in Angular?
Change detection is a mechanism by which Angular determines what changes have occurred in the data and updates the view accordingly.
Angular uses zones and a change detection strategy to optimize rendering by only updating the affected parts of the DOM.
Explain the lifecycle hooks in Angular.
Angular components have a series of lifecycle hooks that provide visibility into the different stages of a component’s lifecycle:
- ngOnChanges(): Called when an input-bound property changes.
- ngOnInit(): Called once the component is initialized.
- ngDoCheck(): Called during every change detection run.
- ngAfterContentInit(): Called after content (ng-content) has been projected.
- ngAfterContentChecked(): Called after every check of the projected content.
- ngAfterViewInit(): Called after the component’s view has been fully initialised.
- ngAfterViewChecked(): Called after every check of the component’s view.
- ngOnDestroy: Called just before the component is destroyed.
What is Angular’s Ahead-of-Time (AOT) Compilation?
AOT compilation is a process in which the Angular compiler converts Angular HTML and TypeScript code into efficient JavaScript code before the browser downloads and runs the application. This leads to faster rendering, fewer asynchronous requests, and better security.
What are Angular modules?
Modules in Angular are containers that group components, directives, pipes, and services, making them cohesive blocks of functionality. Each Angular application has at least one module, the root module, which bootstraps the application. Other feature modules can be created to encapsulate functionality.
What are observables, and how are they used in Angular?
Observables are used for handling asynchronous data streams and events in Angular. The HttpClient service, for example, returns observables when making HTTP requests. Observables can be subscribed to and used with operators such as map, filter, and mergeMap from the RxJS library to transform or combine data.
How would you handle forms in Angular?
Angular provides two approaches to handling forms:
- Template-driven forms: Suitable for simple forms, managed using Angular directives such as ngModel.
- Reactive forms: Suitable for complex forms, managed programmatically using FormGroup, FormControl, and FormBuilder.
What is lazy loading in Angular?
Lazy loading is a technique in which modules are loaded on demand rather than all at once. It reduces the initial load time of an application by only loading the required modules for a particular route. This is achieved using Angular’s router configuration.
What is Angular Material?
Angular Material is a UI component library that follows Google’s Material Design principles. It provides pre-built, customisable components like buttons, cards, dialogs, and tables to enhance the UI and UX of Angular applications.
What is a router in Angular?
Angular’s router is a tool that enables navigation between different views or pages of an application. It provides route configurations, route guards, and parameterised routes, making it easy to manage navigation and route changes in a single-page application.
What are Templates in Angular?
Angular Templates are written with HTML that contains Angular-specific elements and attributes. In combination with the model and controller’s information, these templates are further rendered to provide a dynamic view to the user.
What are Annotations in Angular?
Annotations in Angular create an annotation array. They are the metadata set in the class that reflects the metadata library.
What are Directives in Angular?
Directives are attributes that allow the user to write new HTML syntax specific to their applications. They execute whenever the Angular compiler finds them in the DOM. Angular supports three types of directives.
- Component Directives
- Structural Directives
- Attribute Directives
What are Pipes in Angular?
Pipes are simple functions designed to accept an input value, process, and return as an output, a transformed value in a more technical understanding. Angular supports several built-in pipes. However, you can also create custom pipes that cater to your needs.
Some key features include:
- Pipes are defined using the pipe “|” symbol.
- Pipes can be chained with other pipes.
- Pipes can be provided with arguments by using the colon (:) sign.
What is the PipeTransform interface?
As the name suggests, the interface receives an input value and transforms it into the desired format with a transform() method. It is typically used to implement custom pipes.
import { Pipe, PipeTransform } from ‘@angular/core’;
@Pipe({
name: ‘demopipe’
})
export class DemopipePipe implements PipeTransform {
transform(value: unknown, …args: unknown[]): unknown {
return null;
}
}
What are Pure Pipes?
These pipes are pipes that use pure functions. As a result of this, a pure pipe doesn’t use any internal state, and the output remains the same as long as the parameters passed stay the same. Angular calls the pipe only when it detects a change in the parameters being passed. A single instance of the pure pipe is used throughout all components.