-
What is Angular?
- Angular is a TypeScript-based open-source web application framework developed by Google. It is used for building single-page client applications (SPAs) and provides a structured way of creating dynamic web applications.
-
What are the key features of Angular 11?
- Angular 11 introduced several new features and improvements, including stricter type checking, webpack 5 support, faster rebuilds, updated language service preview, and optional stricter settings for template type checking.
-
What is TypeScript, and why is it used in Angular?
- TypeScript is a superset of JavaScript that adds optional static types to JavaScript code. It is used in Angular for enhanced tooling, better error detection during development, and to leverage features like classes, interfaces, and modules.
-
Explain Angular CLI and its advantages.
- Angular CLI (Command Line Interface) is a command-line tool used to automate development workflows in Angular projects. It provides commands to create components, services, modules, and other Angular artifacts, and simplifies tasks like building, testing, and deploying applications.
-
What are components in Angular? How are they structured?
- Components are the fundamental building blocks of Angular applications, representing a part of the user interface (UI). They consist of a TypeScript class with a
@Component decorator that includes metadata such as HTML templates, styles, and selectors.
-
What is data binding in Angular? Explain the different types of data binding.
- Data binding in Angular allows you to establish a connection between the UI and the business logic of an application. Types of data binding include:
- Interpolation (
{{ }}): Binding data from the component to the view.
- Property binding (
[]): Binding data from the component to an HTML element property.
- Event binding (
()): Binding events emitted by HTML elements to methods in the component.
- Two-way binding (
[()]): Combining property binding and event binding to achieve bi-directional data flow.
-
What are directives in Angular? Explain the different types of directives.
- Directives are markers on a DOM element that tell Angular to attach a specific behavior to that element or transform the DOM structure. Types of directives in Angular include:
- Component Directives: Directives with a template (e.g.,
@Component).
- Attribute Directives: Change the appearance or behavior of an element, component, or another directive (e.g.,
ngClass, ngStyle).
- Structural Directives: Change the DOM layout by adding or removing elements (e.g.,
ngIf, ngFor).
-
Explain Angular modules and their purpose.
- Angular modules (NgModule) are containers for organizing an Angular application into cohesive blocks of functionality. They group related components, directives, pipes, and services into functional sets that can be imported and used throughout the application.
-
What is dependency injection (DI) in Angular? Why is it used?
- Dependency injection is a design pattern and technique used in Angular to inject dependencies (services or objects) into a class, instead of the class creating its dependencies. It promotes code reusability, modularity, and testability by enabling loose coupling between components.
-
What is RxJS in Angular? Explain its role.
- RxJS (Reactive Extensions for JavaScript) is a library for reactive programming using Observables. It is heavily used in Angular for handling asynchronous operations, event handling, and managing streams of data. RxJS operators allow developers to compose complex asynchronous operations with ease.
-
What are pipes in Angular? Give examples of built-in pipes and custom pipes.
- Pipes are a feature in Angular that allows you to transform data before displaying it in the view. Built-in pipes include
DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe, and DecimalPipe. Custom pipes are created using the @Pipe decorator and implementing the PipeTransform interface.
-
Explain Angular routing. How do you implement routing in Angular applications?
- Angular routing is a mechanism for navigating between different components/views in a single-page application. It uses the
RouterModule and Routes array to define routes, which are mapped to specific components and associated with paths and optional parameters.
-
What is lazy loading in Angular? How do you implement lazy loading?
- Lazy loading is a technique in Angular where modules or components are loaded asynchronously when needed, rather than loading them all at once when the application starts. It improves application performance by reducing initial bundle size. Lazy loading is implemented using the
loadChildren property in the route configuration.
-
What are Angular forms? Explain the different types of forms and their use cases.
- Angular forms are used to collect and validate user input in an application. Types of Angular forms include:
- Template-driven forms: Uses directives like
ngModel for two-way data binding within the template.
- Reactive forms: Uses FormBuilder service and FormControl/FormGroup classes to build forms programmatically with explicit data handling and validation.
-
What are Angular services? How do you create and use services in Angular?
- Angular services are singleton objects that are used to encapsulate reusable logic and data across components. They are injectable via dependency injection and provide a way to share data and functionality between components without tight coupling.
-
Explain Angular lifecycle hooks. Give examples of lifecycle hooks and their use cases.
- Angular lifecycle hooks are methods that allow you to tap into key moments in a component's lifecycle. Examples include
ngOnInit, ngOnChanges, ngAfterViewInit, ngOnDestroy, etc. They are used for initialization, data fetching, DOM manipulation, cleanup, and integration with third-party libraries.
-
How do you handle HTTP requests in Angular? Explain the HttpClientModule and HttpClient.
- HTTP requests in Angular are handled using the
HttpClientModule and HttpClient service. HttpClientModule is imported into the application module to provide the HttpClient service, which allows making HTTP requests to a server, handling responses, error handling, and setting headers.
Application Lifecycle
- Explain the Angular application lifecycle.
- The Angular application lifecycle consists of several key stages:
- Bootstrap: Initializes the application.
- Compiling Components: Processes templates and creates component factories.
- Creating Components: Instantiates components along with their dependencies.
- Running Change Detection: Checks for changes in data-bound properties and updates the view.
- Destroying Components: Cleans up resources when components are destroyed.
- What are Angular lifecycle hooks? Give examples of lifecycle hooks and when you would use them.
- Angular lifecycle hooks are methods that Angular calls at certain points in the lifecycle of a component or directive. Examples include
ngOnInit, ngOnChanges, ngAfterViewInit, ngOnDestroy, etc. They are used for initialization, reacting to changes, performing cleanup, and integrating with external libraries.
- How does change detection work in Angular? Explain the default change detection strategy and how you can customize it.
- Change detection in Angular determines when and how to update the DOM based on changes in the application state. By default, Angular uses Zone.js to intercept asynchronous operations and trigger change detection. Strategies include
OnPush for optimizing performance by checking only components marked with ChangeDetectionStrategy.OnPush.
Routing
- Explain Angular routing and its benefits. How do you implement routing in an Angular application?
- Angular routing allows navigation between different views/components within a single-page application (SPA). It uses the
RouterModule and Routes array to define routes with paths and associated components. Benefits include lazy loading, deep linking, and separation of concerns.
- What are nested routes in Angular? How do you implement them?
- Nested routes in Angular allow you to define child routes within parent routes, enabling hierarchical navigation. They are implemented by nesting
children arrays within route configurations. Nested routes facilitate modularization and organization of application features.
- Explain Angular guards. What types of guards are available and when would you use each?
- Angular guards are used to control navigation and access to routes based on conditions. Types include:
- CanActivate: Determines if a route can be activated.
- CanActivateChild: Determines if child routes can be activated.
- CanDeactivate: Determines if a route can be deactivated.
- CanLoad: Prevents lazy-loaded modules from loading.
Modules
- What are Angular modules (NgModules)? Explain the purpose of NgModule and how you organize them in an Angular application.
- Angular modules (NgModules) are containers that group related components, directives, pipes, and services into cohesive units of functionality. They are used to organize an Angular application into manageable modules for modularity, lazy loading, and feature encapsulation.
- What is lazy loading in Angular modules? How do you implement lazy loading?
- Lazy loading is a technique in Angular where modules or components are loaded asynchronously when needed, rather than loading them all at once when the application starts. It is implemented by configuring routes with the
loadChildren property in the RouterModule configuration.
End-to-End Application Architecture
- Explain the architecture of an end-to-end Angular application. What are its components and how do they interact?
- An end-to-end Angular application typically consists of:
- Components: UI elements organized into reusable components.
- Services: Business logic and data access services.
- Modules: NgModules that encapsulate related functionality.
- Routing: Defines navigation paths between components.
- HTTP: Interacts with backend APIs for data exchange.
- These components interact through data binding, event handling, service injection, and routing to provide a seamless user experience.
- Describe how Angular interacts with a backend RESTful API.
- Angular interacts with a backend RESTful API using the
HttpClient service to send HTTP requests (GET, POST, PUT, DELETE) to endpoints defined on the server. It handles responses, error handling, and integrates data fetched from the backend into the application's UI using observables and promises.