UI interview question -Angular

 

What are Single Page Applications (SPA)?

Single Page Applications (SPAs) are web applications that load a single HTML page once and dynamically update the content using JavaScript. This approach enables faster interactions and a smoother, more consistent user experience.

  • Dynamically updates content without reloading the entire page.
  • Provides faster performance and a seamless user experience.

What are templates in Angular?

A template is a kind of HTML that instructs Angular about how to display a component. An Angular HTML template, like conventional HTML, produces a view, or user interface, in the browser, but with far more capabilities. Angular API evaluates an HTML template of a component, creates HTML, and renders it.

There are two ways to create a template in an Angular component:

  • Inline Template
  • Linked Template

Inline Template: The component decorator's template config is used to specify an inline HTML template for a component. The Template will be wrapped inside the single or double quotes.

Linked Template: A component may include an HTML template in a separate HTML file. As illustrated below, the templateUrl option is used to indicate the path of the HTML template file.

What is an AOT Compiler?

The AOT (Ahead-of-Time) Compiler in Angular compiles Angular HTML and TypeScript code into efficient JavaScript before the browser downloads and runs the app. This makes the application load faster, reduces runtime errors, and improves overall performance compared to JIT (Just-in-Time) compilation, which happens in the browser


7. How many types of compilation Angular provides?

Angular provides two types of compilation:

JIT (Just-in-Time) Compilation:

  • Happens at runtime in the browser.
  • Compiles the Angular application in the browser as it loads.
  • Faster development builds but slower performance in production.

AOT (Ahead-of-Time) Compilation:

  • Happens during the build phase before the application is run.
  • Compiles the application into efficient JavaScript code ahead of time, which leads to faster loading and better performance.
  • Recommended for production builds.

What is a component in Angular?

A component is a fundamental building block of Angular applications. It controls a part of the user interface and manages the data and logic for that section. Components are used to create reusable UI elements and define the structure and behavior of the app.


What is a module in Angular?

A module is a logical unit of the application that groups related components, directives, pipes, and services. It helps organize and manage the application by encapsulating functionality into cohesive blocks.


11. What is Angular CLI?

Angular CLI (Command Line Interface) is a powerful tool that helps automate and streamline the development process for Angular applications. It provides a set of commands for creating, managing, and building Angular projects.

Common Angular CLI commands include:

  • ng new: Creates a new Angular project.
  • ng serve: Serves the application locally.
  • ng generate: Generates components, services, and more.
  • ng build: Builds the application for production.

13. What is a service in Angular?

A service is a class that encapsulates reusable logic, which can be shared across different components of an Angular application. Services are typically used for data fetching, business logic, and other operations that need to be shared.

What is a directive in Angular?

Directives are special markers on a DOM element that tell Angular to do something to that DOM element or its children. Directives are used to extend HTML functionality by adding behavior to elements, such as manipulating their attribute or styling

15. What are Angular Lifecycle Hooks?

Angular lifecycle hooks are methods that allow developers to tap into key moments in a component’s lifecycle. Key hooks include 

ngOnInit (called once when the component is initialized),

ngOnChanges (called when input properties change), and 

ngOnDestroy (called before Angular destroys the component).

18. Differences between one-way binding and two-way binding?

In Angular, both one-way and two-way data binding are supported. Angular provides mechanisms for both types of binding based on the use case


What is string interpolation in AngularJS?

String interpolation is a technique used to bind data from the model (JavaScript) to the view (HTML) by embedding expressions within double curly braces {{ }}. It allows you to insert dynamic values or variables into the HTML content, making the view update automatically when the model changes.

<div>{{message}} <div>

 What is the digest cycle in AngularJS?

The digest cycle in AngularJS is a process where Angular compares the current and previous values of the scope model to check for changes. If changes are detected, Angular updates the view. This cycle is triggered automatically after an event like a user action, HTTP request, or model change, and it ensures that the view stays in sync with the model. It can also be manually triggered using $apply().

What is dependency injection in Angular?

Dependency Injection (DI) in Angular is a design pattern where services or objects are provided to components or other services rather than being created within them. It allows for better modularity, testability, and management of dependencies. Angular's DI framework automatically injects required services into components, making it easier to manage and maintain the application.

 What is an Angular router?

The Angular router is a library that enables navigation between different views or pages in a single-page application (SPA). It allows developers to define routes, handle URL changes, and load components dynamically based on the route, providing a smooth and efficient user experience without page reloads.


27. What type of DOM is used in Angular?

Angular uses the Real DOM (Document Object Model). The Change Detection mechanism is used to update only the affected parts of the DOM when data changes, improving performance. In addition, Angular uses a Shadow DOM for encapsulation, which helps isolate styles and behavior of components.

  • Real DOM: Updates the entire DOM when changes occur.
  • Change Detection: Optimizes updates to only parts of the DOM that need re-rendering.
  • Shadow DOM: Provides component style and behavior encapsulation.

29. How can you pass data between components in Angular?

Data can be passed between components using Input and Output decorators, services, or router state.

Passing data from a parent component to a child component using @Input decorator.

export class ChildComponent {
    @Input() childData: string; // Declare the input property
}

Explain lazy loading in Angular?

Lazy loading in Angular is a technique used to improve the performance of an application by loading feature modules only when they are needed, rather than loading all modules upfront. This reduces the initial load time of the application and speeds up the startup process.


What is MVVM architecture in Angular?

MVVM (Model-View-ViewModel) is a software architectural pattern that is commonly used in Angular applications, providing a clean separation of concerns between different components of an application. This ensures that changes in one part of the application (like in the logic or data) do not directly affect or interfere with the user interface.

view_model_

What is a pipe in Angular?

pipe is a way to transform data in the template. It allows you to format or manipulate data before displaying it to the user. Angular provides several built-in pipes like DatePipe, UpperCasePipe, and CurrencyPipe, and you can also create custom pipes. 

How do you implement authentication in Angular?

Authentication can be implemented using JWT tokens, Angular guards, and interceptors to manage login and secure routes.

What is the purpose of the Signal API?

The Signal API in Angular simplifies state management by providing reactive signals that automatically track changes and update the view. This reduces the need for manual change detection, improves performance, and makes applications more responsive and efficient.

  • Automatic tracking of state changes and dependencies.
  • Updates the view without manual change detection.
  • Reduces complexity in managing reactivity.
  • Optimizes performance by minimizing unnecessary updates.
  • 47. What is Ivy in Angular?

    Ivy is Angular's next-generation rendering engine, introduced to improve performance and reduce bundle sizes. It offers faster compilation, more efficient rendering, and enhanced debugging capabilities.

  •  What is Angular Material?

    Angular Material is a UI component library for Angular applications that provides pre-built, reusable, and customizable user interface components, following Google’s Material Design principles.

63. What is Data Binding in Angular?

Data binding in Angular is a mechanism that allows communication between the component and the view (HTML template). It synchronizes the data between the model (component) and the view, making it easy to reflect changes in the UI whenever data changes in the component.

There are the 4 types of the data binding in Agular:

  • Interpolation (One-way Binding)
  • Property Binding (One-way Binding)
  • Event Binding (One-way Binding)
  • Two-way Binding

Question: Your Angular application is getting slower due to a large number of modules and components. How would you optimize the application’s performance?

Answer: One way to optimize an Angular application is by implementing lazy loading to load modules only when needed. This reduces the initial bundle size, improving load times. Here's an example of setting up lazy loading in Angular:

// app-routing.module.ts
const routes: Routes = [
    { path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) }
];
  • By using the loadChildren property, Angular will load the FeatureModule only when the user navigates to the /feature route.
  • This improves the app's performance by deferring the loading of non-essential modules.

Question: We have a form where the user enters their email, and we need to ensure that it is both valid and unique (not already in use). How would we implement this validation using Angular Reactive Forms?

Answer: I would use Reactive Forms with custom synchronous and asynchronous validators. Here's how I would implement both email format validation and uniqueness check:

How does an Angular application work?

Every Angular app consists of a file named angular.json. This file will contain all the configurations of the app. While building the app, the builder looks at this file to find the entry point of the application

Write a code where you have to share data from the Parent to Child Component?

You have to share the data amongst the components in numerous situations. It may consist of unrelated, parent-child, or child-parent components.


The @Input decorator allows any data to be sent from parent to child.


// parent component

import { Component } from '@angular/core';

@Component({

  selector: 'app-parent',

  template: `

<app-child [childMessage]="parentMessage"></app-child>

`,

  styleUrls: ['./parent.component.css']

})

export class ParentComponent{

  parentMessage = "message from parent"

  constructor() { }

}

// child component

import { Component, Input } from '@angular/core';

@Component({

  selector: 'app-child',

  template: `Say {{ childMessage }}`,

  styleUrls: ['./child.component.css']

})

export class ChildComponent {

  @Input() childMessage: string;

  constructor() { }

}



Comments

Popular posts from this blog

Archunit test

Hexagonal Architecture

visitor design pattern