Angular In-Depth Flashcards

These questions explore more advanced Angular concepts and can help demonstrate your in-depth understanding of the framework during interviews.

1
Q

What are directives in Angular?

A

Directives are classes in Angular that allow you to modify the behavior or appearance of elements in the DOM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What types of directives are there?

A

Component Directives:
These are the most common and define the structure and behavior of a component.

Structural Directives:
These alter the DOM layout by adding or removing elements (e.g., *ngIf, *ngFor).

Attribute Directives:
These change the appearance or behavior of an element (e.g., ngClass, ngStyle).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the role of NgModule in Angular?

A

NgModule is a decorator that declares an Angular module. It organizes an application into cohesive blocks by defining components, directives, pipes, and services that belong to the module. It also helps configure providers and routing. Every Angular app has at least one root module (AppModule).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is two-way data binding in Angular, and how is it achieved?

A

Two-way data binding allows data to flow from the component to the view and vice versa. In Angular, it is achieved using the [(ngModel)] directive. It combines property binding ([ ]) and event binding (( )) into a single expression, keeping the view and model in sync.

<input [(ngModel)]=”username” />

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are pipes in Angular, and how do you use them?

A

Pipes in Angular are used to transform data in templates. For example, you can use pipes to format dates, currencies, or strings. Angular provides built-in pipes like | date, | uppercase, | lowercase, and you can also create custom pipes by implementing the PipeTransform interface.

<p>{{ user.name | uppercase }}</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you handle HTTP requests in Angular?

A

Angular provides the HttpClient module for handling HTTP requests. You can import HttpClientModule in your AppModule and use HttpClient to send GET, POST, PUT, and DELETE requests. It returns an observable, making it easy to handle asynchronous data.

Example of a GET request:
this.http.get(‘api/users’).subscribe((data) => console.log(data));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a resolver in Angular routing?

A

A resolver in Angular is used to prefetch data before the route is activated. It is a service that implements the Resolve interface and is used to retrieve necessary data before rendering the component. Resolvers can prevent a component from loading until the data is available.

export class UserResolver implements Resolve<User> {
resolve(route: ActivatedRouteSnapshot): Observable<User> {
return this.userService.getUser(route.paramMap.get('id'));
}
}</User></User>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are route guards in Angular?

A

Route guards are interfaces that control navigation to and from routes based on conditions. There are several types of guards:

  • CanActivate: Decides if a route can be activated.
  • CanDeactivate: Decides if a route can be left.
  • Resolve: Pre-fetches data before the route loads.
  • CanLoad: Determines if a module can be loaded lazily.

Guards are implemented as services that return true, false, or an observable/promise that resolves to true or false.

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.authService.isAuthenticated();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between ng-template and ng-container?

A

ng-template:
This element is used to define an Angular template that isn’t rendered immediately but can be displayed later by Angular with structural directives like *ngIf or *ngFor.

ng-container:
This is a grouping element that doesn’t create an actual DOM element. It’s useful for grouping multiple elements or applying structural directives without adding unnecessary nodes to the DOM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is ViewChild in Angular, and how is it used?

A

ViewChild is a decorator that allows access to a child component, directive, or DOM element from the parent component’s class. It is used to interact with child elements and manipulate their properties or methods.

@ViewChild(‘childComponent’) child: ChildComponent;

ngAfterViewInit() {
this.child.someMethod();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of Angular’s async pipe?

A

The async pipe is used to subscribe to an observable or promise in a template, eliminating the need to subscribe manually in the component class. It automatically unsubscribes when the component is destroyed, which helps prevent memory leaks.

<p>{{ user$ | async }}</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between AOT and JIT?

A

Ahead of Time (AOT) compilation converts your code during the build time before the browser downloads and runs that code. This ensures faster rendering to the browser. To specify AOT compilation, include the –aot option with the ng build or ng serve command.

The Just-in-Time (JIT) compilation process is a way of compiling computer code to machine code during execution or run time. It is also known as dynamic compilation. JIT compilation is the default when you run the ng build or ng serve CLI commands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain the @Component Decorator.

A

TypeScript class is one that is used to create components. This genre of class is then decorated with the “@Component” decorator. The decorato’s purpose is to accept a metadata object that provides relevant information about the component.

import { Component }. from ‘@angular/core’;

@Component({
selector: ‘app-root’,
templateUrl: ‘./app.component.html’,
styleUrls: [’./app.component.css’]
})

export class Component {
title = ‘example’;
}

The image above shows an App component - a pure TypeScript class decorated with the “@Component” decorator. The metadata object that gets accepted by the decorator provides properties like templateUrl, selector, and others, where the templateUrL property points to an HTML file defining what you see on the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are Services in Angular?

A

Angular Services perform tasks that are used by multiple components. These tasks could be data and image fetching, network connections, and database management among others. They perform all the operational tasks for the components and avoid rewriting of code. A service can be written once and injected into all the components that use that service.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are Promises and Observables in Angular?

A

While both the concepts deal with Asynchronous events in Angular, Promises handle one such event at a time while observables handle a sequence of events over some time.

Promises - They emit a single value at a time. They execute immediately after creation and are not cancellable. They are Push errors to the child promises.

Observables - They are only executed when subscribed to them using the subscribe() method. They emit multiple values over a period of time. They help perform operations like forEach, filter, and retry, among others. They deliver errors to the subscribers. When the unsubscribe() method is called, the listener stops receiving further values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is ngOnInit? How is it defined?

A

ngOnInit is a lifecycle hook and a callback method that is run by Angular to indicate that a component has been created. It takes no parameters and returns a void type.

export class MyComponent implements OnInit {

constructor() { }

ngOnInit(): void {
//….
}
}

17
Q

How to use ngFor in a tag?

A

The ngFor directive is used to build lists and tables in the HTML templates. In simple terms, this directive is used to iterate over an array or an object and create a template for each element.

<ul>
<li *ngFor = "let items in itemlist"> {{ item }} </li>
</ul>

  1. “Let item” creates a local variable that will be available in the template
  2. “Of items” indicates that we are iterating over the items iterable.
  3. The * before ngFor creates a parent template.
18
Q

What are Template forms?

A

Template-driven approach

  • In this method, the conventional form tag is used to create forms. Angular automatically interprets and creates a form object representation for the tag.
  • Controls can be added to the form using the NGModel tag. Multiple controls can be grouped using the NGControlGroup module.
  • A form value can be generated using the “form.value” object. Form data is exported as JSON values when the submit method is called.
  • Basic HTML validations can be used to validate the form fields. In the case of custom validations, directives can be used.
  • Arguably, this method is the simplest way to create an Angular App.
19
Q

What are Reactive forms?

A

Reactive Form Approach

  • This approach is the programming paradigm oriented around data flows and propagation of change.
  • With Reactive forms, the component directly manages the data flows between the form controls and the data models.
  • Reactive forms are code-driven, unlike the template-driven approach.
  • Reactive forms break from the traditional declarative approach.
  • Reactive forms eliminate the anti-pattern of updating the data model via two-way data binding.
  • Typically, Reactive form control creation is synchronous and can be unit tested with synchronous programming techniques.
20
Q

What is Bootstrap? How is it embedded into Angular?

A

Bootstrap is a powerful toolkit. It is a collection of HTML, CSS, and JavaScript tools for creating and building responsive web pages and web applications.

There are two ways to embed the bootstrap library into your application.

  1. Angular Bootstrap via CDN - Bootstrap CDN is a public Content Delivery Network. It enables you to load the CSS and JavaScript files remotely from its servers.
  2. Angular Bootstrap via NPM - Another way to add Bootstrap to your Angular project is to install it into your project folder by using NPM (Node Package Manager).

npm install bootstrap
npm install jquery

21
Q

What type of DOM does Angular implement?

A

DOM (Document Object Model) treats an XML or HTML document as a tree structure in which each node is an object representing a part of the document.

Angular uses the regular DOM. This updates the entire tree structure of HTML tags until it reaches the data to be updated. However, to ensure that the speed and performance are not affected, Angular implements Change Detection.

With this, you have reached the end of the article. We highly recommend brushing up on the core concepts for an interview. It’s always an added advantage to write the code in places necessary.

21
Q

What is Eager and Lazy loading?

A

Eager loading is the default module-loading strategy. Feature modules under Eager loading are loaded before the application starts. This is typically used for small size applications.

Lazy loading dynamically loads the feature modules when there’s a demand. This makes the application faster. It is used for bigger applications where all the modules are not required at the start of the application.

22
Q

Why were client-side frameworks like Angular introduced?

A

Client-side frameworks like Angular were introduced to provide a more responsive user experience. By using a framework, developers can create web applications that are more interactive and therefore provide a better user experience.

Frameworks like Angular also make it easier for developers to create single-page applications (SPAs). SPAs are web applications that only need to load a single HTML page. This makes them much faster and more responsive than traditional web applications.

Overall, client-side frameworks like Angular were introduced in order to improve the user experience of web applications. By making web applications more responsive and easier to develop, they provide a better experience for both developers and users.

23
Q

How are Angular expressions different from JavaScript expressions?

A

One major difference between Angular expressions and JavaScript expressions is that Angular expressions are compiled while JavaScript expressions are not. This means that Angular expressions are more efficient since they’re already pre-processed. Additionally, Angular expressions can access scope properties while JavaScript expressions cannot. Finally, Angular expressions support some additional features such as filters and directives which aren’t available in JavaScript expressions.

24
Q

Explain components, modules and services in Angular.

A

Components, modules and services are the three fundamental building blocks in Angular. Components are the smallest, self-contained units in an Angular application. They are typically used to represent a view or UI element, such as a button or a form input field.

Modules are larger units that group together one or more related components. Services are singleton objects that provide specific functionality throughout an Angular application, such as data access or logging.

Each component in Angular has its own isolated scope. This means that a component’s dependencies (services, other components, etc.) are not accessible to any other component outside of its own scope. This isolation is important for ensuring modularity and flexibility in an Angular application.

Services, on the other hand, are not isolated and can be injected into any other unit in an Angular application (component, module, service, etc.). This makes them ideal for sharing data or functionality across the entire app.

When designing an Angular application, it is important to keep these three building blocks in mind. Components should be small and self-contained, modules should group together related components, and services should provide shared functionality across the entire app. By following this design principle, you can create an Angular application that is modular, flexible, and easy to maintain.

25
Q

Angular by default, uses client-side rendering for its applications.

A

This means that the Angular application is rendered on the client-side — in the user’s web browser. Client-side rendering has a number of advantages, including improved performance and better security. However, there are some drawbacks to using client-side rendering, as well. One of the biggest drawbacks is that it can make your application more difficult to debug.

Client-side rendering is typically used for applications that are not heavily data-driven. If your application relies on a lot of data from a server, client-side rendering can be very slow. Additionally, if you’re using client-side rendering, it’s important to be careful about how you load and cache your data. If you’re not careful, you can easily end up with an application that is very slow and difficult to use. When rendered on the server-side, this is called Angular Universal.

26
Q

How do you share data between components in Angular?

A

Sharing data between components in Angular is simple and easy. To share data, all you need to do is use the Angular CLI to generate a new service. This service can be injected into any component and will allow the components to share data.

27
Q

Explain the concept of dependency injection.

A

Dependency injection is a technique used to create loosely coupled code. It allows pieces of code to be reused without the need for hard-coded dependencies. This makes code more maintainable and easier to test. Dependency injection is often used in frameworks like AngularJS, ReactJS, and VueJS. It is also possible to use dependency injection in vanilla JavaScript. To use dependency injection in JavaScript, you need a dependency injection library. There are many different libraries available, but they all work in basically the same way.

The first step is to create a dependency injection container. This is a simple object that will hold all of the dependencies that your code needs. Next, you need to register all of the dependencies that your code will need with the container. The registration process will vary depending on the library you are using, but it is usually just a matter of providing the dependency’s name and constructor function.

Once all of the dependencies have been registered, you can then inject them into your code. The injection process will again vary depending on the library you are using, but it is usually just a matter of providing the dependency’s name. The library will then take care of instantiating the dependency and passing it to your code.

Dependency injection can be a great way to make your code more modular and easier to maintain. It can also make it easier to unit test your code, as you can inject mock dependencies instead of the real ones. If you are using a framework that supports dependency injection, it is probably already being used in your code. If you are not using a framework, you can still use dependency injection by choosing a library and following the above steps.

28
Q

Explain MVVM architecture.

A

MVVM architecture is an architectural pattern used mainly in software engineering. It stands for Model-View-ViewModel. MVVM is a variation of the traditional MVC (Model-View-Controller) software design pattern. The main difference between the two is that MVVM separates the user interface logic from the business logic, while MVC separates the data access logic from the business logic. This separation of concerns makes it easier to develop, test, and maintain software applications.

The Model layer in MVVM architecture is responsible for storing and managing data. It can be a database, a web service, or a local data source. The View layer is responsible for displaying data to the user. It can be a graphical user interface (GUI), a command-line interface (CLI), or a web page. The ViewModel layer is responsible for handling user input and updating the View layer accordingly. It contains the business logic of the application.

MVVM architecture is often used in conjunction with other software design patterns, such as Model-View-Presenter (MVP) and Model-View-Controller (MVC). These patterns can be used together to create a complete software application.

MVVM architecture is a popular choice for modern software applications. It allows developers to create applications that are more responsive and easier to maintain. Additionally, MVVM architecture can be used to create applications that can be easily ported to different platforms.