In 2024, recession and lay off still is on going. Many developers are looking for a job. This is not a ideal situation you will get experience from your first and second interview and crack the third interview. Right now its difficult to get a interview call. You will prepared yourself so that you can crack your first interview. Last 3 months, I got three interviews. I cracked all of them.
For Angular preparation, I noted down about angular in google drive. I am sharing it in article in medium to help others to prepare for Angular interview. I tried to connect some questions to design patterns and tried to discuss scenario based questions.
ng-container
is a structural directive that doesn't create any additional DOM elements. ng-container
is to provide a grouping mechanism for applying structural directives like ngIf
, ngFor
, and ngSwitch
without introducing unnecessary HTML elements.@Input
decorator) and receive data from child components through output properties (@Output
decorator).ViewChild
and ContentChild
decorators.While it is technically possible to call an API from the Angular constructor, it’s not considered a best practice. The constructor is invoked when the component class is initialized, before Angular has fully initialized certain features like dependency injection or input properties. This means that calling an API before these features are fully available may lead to unexpected behavior or errors.
{{ }}
) and property binding ([ ]
), automatically sanitize user input, making it safe to render in the browser. Always use these mechanisms when rendering dynamic content to prevent XSS.mport { DomSanitizer } from '@angular/platform-browser'; constructor(private sanitizer: DomSanitizer) {} // Sanitize user input sanitizedHtml = this.sanitizer.sanitize(SecurityContext.HTML, userInput);
innerHTML
or outerHTML
to dynamically generate HTML content. Instead, utilize Angular's template syntax to safely render dynamic content.In Angular, @HostBinding and @HostListener decorators are used to interact with the host element of a directive or component.
@HostBinding: The @HostBinding decorator is used to bind a property of the host element. It allows you to set a property on the host element based on a property of the directive or component.
import { Directive, HostBinding } from '@angular/core'; @Directive({ selector: '[appHighlight]' }) export class HighlightDirective { @HostBinding('style.backgroundColor') backgroundColor: string = 'yellow'; }
@HostListener: The @HostListener decorator is used to listen for events on the host element. It allows you to define a method that will be called when a specific event occurs on the host element
import { Directive, HostListener } from '@angular/core'; @Directive({ selector: '[appClickLogger]' }) export class ClickLoggerDirective { @HostListener('click', ['$event']) onClick(event: Event): void { console.log('Host element clicked!', event); } }
This is a simple question, and I know you can finish it in a few minutes. However, the interview’s intention is to understand how deeply you know Angular and what kind of steps you would take to complete this task.
When you face such kind of question try to cover as much as possible like Security, Error handing, Testing, State management, Form validation, API call, Interceptor.
Here is some basic ideas
ErrorHandler
interface to handle the global error. I will use that to handle error in the Angular application. I will also use HttpClientInterceptor to intercept HTTP requests and handle errors returned by the server.There is no right wrong answer this type of question. This is the sample points that will help you to prepare yourself.
There is 90% chance to face this question. The fun fact is that from amateur to advanced developer everybody knows the answer. The important part is how you delivery the answer make a difference from amateur. I always try to connect with my previous work experience.
Here is my answer:
This is a important question. There are 8 life cycle hooks in Angular. I have heavily used ngOnInit and ngOnDestroy in my previous work environment. ngOnInit is called when component initialize. It is called once. Mostly, I used for variable initialize and API call. ngOnDestroy is called before detroying the component. I heavily used for unsubscribe the subscription to prevent the memory leak.
Several times I have used ngOnChanges, ngAfterContentInit, ngAfterViewInit in my career. ngOnChanges method is called once on component’s creation and then every time changes are detected in one of the component’s input properties. It receives a SimpleChanges object as a parameter. ngAfterViewInit is called after the component view and its child views has been initialized. ngAfterContentInit is called after components external content (or from parent ) has been initialized.
There are other hooks like ngDoCheck, ngAfterContentChecked, ngAfterViewChecked, I did not use them too much.
Whenever you have chance try to connect with design pattern. Try to add extra information in your answer that makes you are senior or experienced developer.
Here is my answer:
Dependency Injection (DI) is a design pattern. DI is heavily used in Angular. DI is a design pattern that aims to manage component dependencies by injecting them from external sources rather than creating them within the component itself. It is use to improved testability, make Components loose coupling.
By default, Angular’s DI system creates singleton instances of services and shares them throughout the application. In AngularJs, function params are used for DI, From Angular 2, constructor param is used for DI.
I faced this question several times. I never answered this question like 7 years. I tried to make it informative to get the interviewer attention.
Here is my answer:
I started to work with Angular from Angular 2. Its from 2017. Angular 2 was released in September 2016. After that Angular team started to release new version in every 6 months. In Angular 5, they introduced httpClient. Angular 8 they introduce Ivy engine to make bundle size small. Ivy engine make it default in Angular 9. Then Angular 15, they introduce standalone component. Angular 17, They make it default. Angular 17, they have introduced Built-in control flow, Deferrable views, Angular Signals.
We can solve it different way. We can use if else to handle the scenario for USA and European.
My suggestions are to use separate env files. For USA, we will have environment.usa.ts for European, we will have environment.european.ts . When we build the angular application for production we will select the environment name during build time.
ng build --prod --configuration=usa
or
ng build --prod --configuration=european
We will deploy the build files to corresponding server.
Here is some common questions that you should prepare yourself before interview
In conclusion, I tried to describe my answer that will help you to prepare your answer. Good luck.