Interview question and answers for Regular Angular developer

See the list of interview questions and answers for a regular Angular developer position. The questions cover a good understanding of the Angular framework

December 29, 2022 angular

Who is a Regular Angular developer?

A regular Angular developer is someone who has more experience working with Angular and has a deeper understanding of the framework and web development principles. Is able to work independently and is able to complete tasks and projects with minimal guidance. A regular Angular developer has the ability to troubleshoot and solve complex problems.

What is trackBy and how it works with NgFor?

The trackBy function allows Angular to track the items in an array and only re-render the DOM elements that have changed. This can significantly improve the performance of *ngFor when working with large lists which often change.

A custom trackBy function must have several properties:

A common use for custom trackBy a function is when the model that NgFor iterates over containing a property with a unique identifier. For example:

1
import { Component } from "@angular/core";
2
3
interface User {
4
id: number;
5
name: string;
6
}
7
8
@Component({
9
selector: "my-app",
10
template: `
11
<ul>
12
<li *ngFor="let item of items; trackBy: trackByFn">
13
{{ item.name }}
14
</li>
15
</ul>
16
`,
17
})
18
export class AppComponent {
19
items: User[] = [
20
/* ... */
21
];
22
23
trackByFn(index: number, item: User): number {
24
return item.id; // assume each item has a unique 'id' property
25
}
26
}

What is forRoot and forChild import?

If a module defines both providers and declarations (components, directives, pipes), then loading the module in multiple feature modules would duplicate the registration of the service. This could result in multiple service instances and the service would no longer behave as a singleton.

In the Angular framework, the forRoot() and forChild() are conventional names for methods which are used when configuring the providers of a module:

1
// A example module with forRoot and forChild static methods
2
3
@NgModule({
4
// ...
5
})
6
export class SomeModule {
7
static forRoot(): ModuleWithProviders {
8
return {
9
ngModule: SomeModule,
10
providers: [SomeService],
11
};
12
}
13
14
static forChild(): ModuleWithProviders {
15
return {
16
ngModule: SomeModule,
17
providers: [],
18
};
19
}
20
}

How change detector works in Angular?

In Angular, the change detection system is responsible for detecting changes in the component tree and updating the view to reflect those changes. It does this by running a process called change detection that compares the current component tree to a previous snapshot of the tree and identifies any changes that have occurred.

The change detection process begins when an event triggers an update to the component tree:

If a change is detected, Angular updates the component’s view to reflect the change. If no changes are detected, Angular skips the component and moves on to the next component in the tree.

What is a change detection strategy?

In Angular, a change detection strategy determines when the framework should check for changes in the component data and update the view accordingly. There are two main change detection strategies available in Angular.

Default change detection strategy

This is the default strategy used in Angular. Angular runs a tick, during which the change detector compares each value of a component property with its previous value. If the value has changed, Angular updates the view to reflect the change.

OnPush change detection strategy

This is an optimization of the default change detection strategy. With OnPush change detection, Angular only runs change detection when a component’s input properties change or an event is emitted by the component. This can improve the performance of your application, especially if you have a large number of components or if your components have complex relationships with each other. The main trade-off is that you have to manually run a change detector if a change is not emitted by the component.

To use OnPush change detection, you need to set the changeDetection property of the component to ChangeDetectionStrategy.OnPush.

1
import { ChangeDetectionStrategy, Component, ChangeDetectorRef } from "@angular/core";
2
import { timer } from "rxjs";
3
4
// Component with OnPush change detection strategy
5
@Component({
6
selector: "app-my-component",
7
changeDetection: ChangeDetectionStrategy.OnPush,
8
})
9
export class MyComponent {
10
constructor(private changeDetector: ChangeDetectorRef) {}
11
12
ngOnInit() {
13
// because event is not caused by component event
14
// you have to manually run change detection
15
timer(5000).subscribe(() => this.changeDetector.markForCheck());
16
}
17
}

What is NgZone?

Angular uses zones to run its change detection processes. A zone is a sort of execution context that allows Angular to track the asynchronous tasks that are happening in an application and trigger a change detection cycle when needed.

The NgZone service provides methods for running code inside or outside of the Angular zone. This can be useful for optimizing the performance of your application, as change detection cycles can be expensive to run.

For example, you might use the NgZone service to run code that is not related to the component data outside of the Angular zone. This way, Angular will not trigger a change detection cycle when that code is executed, improving the performance of your application.

1
import { Component, NgZone, OnInit } from "@angular/core";
2
import * as Plotly from "plotly";
3
4
@Component({
5
selector: "app-my-component",
6
})
7
class AppComponent implements OnInit {
8
constructor(private ngZone: NgZone) {}
9
10
ngOnInit() {
11
// build plotly outside angular zone
12
// to not run change detection inside library
13
this.ngZone.runOutsideAngular(() => {
14
Plotly.react("myDiv");
15
});
16
}
17
}

Do you like the content?

Your support helps me continue my work. Please consider making a donation.

Donations are accepted through PayPal or Stripe. You do not need a account to donate. All major credit cards are accepted.

Leave a comment