Top 5 Most Asked Angular Interview Questions – 2024
Top 5 Most Asked Angular Interview Questions – 2024
Explain two-way data binding in Angular?
What is Dependency Injection in Angular?
Explain the Angular Component Lifecycle Hooks
What is Angular Routing and how does it work?
Explain Angular Directives and give examples
Explain two-way data binding in Angular?
- Two-way data binding in Angular allows automatic synchronization between the model (component) and the view (template).
- It’s achieved using the
[(ngModel)]
directive, primarily used with forms.
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<input [(ngModel)]="name" placeholder="Enter your name">
<p>Hello, {{ name }}!</p>
`,
})
export class AppComponent {
name: string = 'John Doe';
}
- The
[(ngModel)]
directive binds the input field to thename
property. - Changes in the input field automatically update the
name
property, and vice versa.
What is Dependency Injection in Angular?
- Dependency Injection (DI) is a design pattern where a class receives its dependencies from an external source rather than creating them itself.
- Angular’s DI system is used to manage the dependencies of components, services, and other objects.
// user.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class UserService {
getUsers(): string[] {
return ['User1', 'User2', 'User3'];
}
}
// user-list.component.ts
import { Component } from '@angular/core';
import { UserService } from './user.service';
@Component({
selector: 'app-user-list',
template: `
<h2>User List</h2>
<ul>
<li *ngFor="let user of users">{{ user }}</li>
</ul>
`,
})
export class UserListComponent {
users: string[];
constructor(private userService: UserService) {
this.users = this.userService.getUsers();
}
}
@Injectable()
decorator marks a class as a service that can be injected into other components or services.constructor(private userService: UserService)
injects an instance ofUserService
intoUserListComponent
.
Explain the Angular Component Lifecycle Hooks?
- Angular components go through various phases in their lifecycle.
- Angular provides lifecycle hooks that allow developers to execute code at specific points in the lifecycle.
// lifecycle-example.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-lifecycle-example',
template: '<p>{{ message }}</p>',
})
export class LifecycleExampleComponent implements OnInit {
message: string = 'Component is being initialized';
ngOnInit() {
console.log('Component initialized');
}
}
ngOnInit
is a lifecycle hook called after the component is initialized.- It’s commonly used for initialization logic.
What is Angular Routing and how does it work?
- Angular Routing is a mechanism for navigating between different views (components) in an Angular application.
- It enables the creation of single-page applications by dynamically changing the content based on the route.
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home.component';
import { AboutComponent } from './about.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
// app.component.html
<a routerLink="/">Home</a>
<a routerLink="/about">About</a>
<router-outlet></router-outlet>
RouterModule
is used to configure routes.routerLink
is a directive used for navigation.<router-outlet>
is a placeholder where the routed component will be displayed.
Explain Angular Directives and give examples?
- Angular Directives are instructions in the DOM that tell Angular to attach a behavior to an element or transform the DOM structure.
- They are categorized into structural, attribute, and component directives.
Structural Directives:
// app.component.html
<div *ngIf="isLoggedIn; else notLoggedIn">
Welcome, User!
</div>
<ng-template #notLoggedIn>
Please log in.
</ng-template>
Attribute Directives:
// highlight.directive.ts
import { Directive, ElementRef, Renderer2, Input } from '@angular/core';
@Directive({
selector: '[appHighlight]',
})
export class HighlightDirective {
@Input() highlightColor: string = 'yellow';
ngOnChanges() {
this.renderer.setStyle(this.el.nativeElement, 'background-color', this.highlightColor);
}
constructor(private el: ElementRef, private renderer: Renderer2) {}
}
// app.component.html
<p appHighlight [highlightColor]="'cyan'">Highlighted in cyan</p>
- tructural directives like
*ngIf
modify the structure of the DOM. - Attribute directives like
appHighlight
modify the behavior or appearance of an element.
More about directives
- Structural Directives:
- Definition: Structural directives alter the structure of the DOM by adding or removing elements based on conditions.
- Examples:
*ngIf
: Conditionally adds or removes elements based on a boolean expression.*ngFor
: Iterates over a collection and generates elements for each item.
- Attribute Directives:
- Definition: Attribute directives change the appearance or behavior of an existing DOM element.
- Examples:
ngStyle
: Dynamically applies styles to an element based on a condition.ngClass
: Dynamically applies CSS classes to an element based on conditions.
- Component Directives:
- Definition: Components themselves can be considered a type of directive since they have templates and controllers.
- Examples:
- Custom components that encapsulate a specific behavior or UI element.
Angular Interview Questions for Beginners with Answers
Common Angular Interview Questions and Practical Examples
Angular Development Interview Q&A for Junior Developers
Lesser-Known Angular Interview Questions for Freshers
Angular Framework Interview Questions Demystified