If you are preparing for an Angular interview, it’s essential to be familiar with the most commonly asked questions. To help you prepare thoroughly, we’ve compiled a comprehensive list of Angular interview questions and answers. Whether you’re a beginner or an experienced developer, this post will provide valuable insights to help you land your dream job.
Mostly asked Angular interview questions and answers
Q1: What is Angular and how does it work?
A: Angular is a JavaScript framework for building web applications. It allows you to create dynamic and interactive web pages by combining HTML, CSS, and JavaScript. Angular uses a component-based architecture to build reusable and modular UI components. Here is an example of a simple Angular component:
import { Component } from '@angular/core';
@Component({
selector: 'app-greeting',
template: '<h1>Hello {{ name }}!</h1>'
})
export class GreetingComponent {
name = 'World';
}
In this example, the GreetingComponent
class defines a template that displays a greeting with the name
property. The @Component
decorator provides metadata for the component, including the selector
property which specifies how the component is used in other templates.
Also read: Reactive Forms Interview Questions and Answers for Angular Developers
Q2: What is data binding in Angular?
A: Data binding is a way to connect the data in your application to the user interface. There are several types of data binding in Angular:
Interpolation: Used to display values from the component in the template.
Example:
<h1>Hello {{ name }}!</h1>
Property binding: Used to set values of HTML attributes or properties.
Example:
<img [src]="imageUrl" alt="Image">
Event binding: Used to listen to events and trigger actions in the component.
Example:
<button (click)="onButtonClick()">Click me</button>
Two-way binding: Used to synchronize values between the component and the template.
Example:
<input [(ngModel)]="name" placeholder="Name">
Q3: How do you create a service in Angular?
A: A service in Angular is a class that provides functionality to multiple components. Here is an example of how to create a simple service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MyService {
getData() {
return ['apple', 'banana', 'orange'];
}
}
In this example, the MyService
class defines a getData
method that returns an array of fruits. The @Injectable
decorator provides metadata for the service, including the providedIn
property which specifies the root injector as the provider of the service.
Q4: How do you use HTTP in Angular to make API calls?
A: Angular provides a built-in HttpClient
module to make HTTP requests. Here is an example of how to use HttpClient
to make a GET request to an API:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ApiService {
apiUrl = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: HttpClient) {}
getPosts() {
return this.http.get(this.apiUrl);
}
}
In this example, the ApiService
class defines a getPosts
method that makes a GET request to the JSONPlaceholder API and returns the response as an observable. The HttpClient
service is injected into the constructor of the service using Angular’s dependency injection system.
Q5: How do you create a directive in Angular?
A: A directive in Angular is a way to extend the functionality of HTML elements or components. Here is an example of how to create a simple directive:
import { Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(el: ElementRef) {
el.nativeElement.style.backgroundColor = 'yellow';
}
}
In this example, the HighlightDirective
class defines a directive that sets the background color of an element to yellow. The @Directive
decorator provides metadata for the directive, including the selector
property which specifies the CSS selector for the directive.
Q6: How do you handle forms in Angular?
A: Angular provides two ways to handle forms: template-driven forms and reactive forms. Here is an example of how to create a simple template-driven form:
<form (ngSubmit)="onSubmit()">
<label>
Name:
<input type="text" name="name" [(ngModel)]="name">
</label>
<button type="submit">Submit</button>
</form>
In this example, the form element has an (ngSubmit)
event binding that calls the onSubmit()
method when the form is submitted. The input element uses [(ngModel)]
to bind the value of the input to the name
property of the component.
Q7: How do you create a route in Angular?
A: Angular provides a RouterModule
module to handle routing in your application. Here is an example of how to create a simple route:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home.component';
const routes: Routes = [
{ path: '', component: HomeComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
In this example, the AppRoutingModule
module defines a single route that maps the root URL to the HomeComponent
. The RouterModule.forRoot()
method sets up the router with the routes defined in the routes
array. The RouterModule
is then exported so it can be used in other modules.
Q8: How do you use Angular animations?
A: Angular provides a @angular/animations
module to handle animations in your application. Here is an example of how to create a simple animation:
import { trigger, transition, style, animate } from '@angular/animations';
export const fadeInOutAnimation = trigger('fadeInOut', [
transition(':enter', [
style({ opacity: 0 }),
animate('0.5s ease-out', style({ opacity: 1 }))
]),
transition(':leave', [
animate('0.5s ease-out', style({ opacity: 0 }))
])
]);
In this example, the fadeInOutAnimation
animation defines two transitions: one for entering an element and one for leaving an element. The transitions use the animate()
function to apply styles over time. The animation can be used in a component template by binding to the [@fadeInOut]
property of an element.
Q9: How do you make an HTTP request in Angular?
A: Angular provides the HttpClient
module to make HTTP requests in your application. Here is an example of how to make a simple GET request:
import { HttpClient } from '@angular/common/http';
export class DataService {
constructor(private http: HttpClient) {}
getData() {
return this.http.get('/api/data');
}
}
In this example, the DataService
class injects the HttpClient
service in its constructor. The getData()
method uses the http.get()
method to make a GET request to the /api/data
URL.
Q10: How do you use a service in Angular?
A: A service in Angular is a class that provides functionality to other parts of your application. Here is an example of how to use a service in a component:
import { Component } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-data',
template: `
<ul>
<li *ngFor="let item of data">{{ item }}</li>
</ul>
`
})
export class DataComponent {
data: string[];
constructor(private dataService: DataService) {}
ngOnInit() {
this.dataService.getData().subscribe((data: string[]) => {
this.data = data;
});
}
}
In this example, the DataComponent
component injects the DataService
service in its constructor. The ngOnInit()
lifecycle hook calls the getData()
method of the service and subscribes to the observable to get the data. The data is then stored in the data
property of the component and displayed in the template using *ngFor
.
Q11: How do you use a pipe in Angular?
A: A pipe in Angular is a way to transform data in your application.
Here is an example of how to use a built-in DatePipe
:
import { Component } from '@angular/core';
@Component({
selector: 'app-date',
template: `
<p>Today is {{ today | date }}</p>
`
})
export class DateComponent {
today = new Date();
}
In this example, the DateComponent
component defines a today
property that contains the current date. The date
pipe is used to format the date and display it in the template.
Q12: How do you use dependency injection in Angular?
A: Dependency injection in Angular is a way to provide instances of objects to other objects that need them.
Here is an example of how to use dependency injection in a component:
import { Component } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-data',
template: `
<ul>
<li *ngFor="let item of data">{{ item }}</li>
</ul>
`,
providers: [DataService]
})
export class DataComponent {
data: string[];
constructor(private dataService: DataService) {}
ngOnInit() {
this.dataService.getData().subscribe((data: string[]) => {
this.data = data;
});
}
}
In this example, the DataComponent
component defines a providers
property that specifies the DataService
as a provider. The DataService
is then injected into the component using its constructor. When the component is created, Angular automatically creates an instance of the DataService
and provides it to the component.
Q13: How do you use ngFor in Angular?
A: The ngFor
directive in Angular is used to loop over a collection of items and create a template for each item. Here is an example of how to use ngFor
in a component:
import { Component } from '@angular/core';
@Component({
selector: 'app-list',
template: `
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>
`
})
export class ListComponent {
items = ['Item 1', 'Item 2', 'Item 3'];
}
In this example, the ListComponent
component defines an items
property that contains an array of strings. The *ngFor
directive is used to loop over the items
array and create an <li>
element for each item.
Q14: How do you use ngIf in Angular?
A: The ngIf
directive in Angular is used to conditionally display elements based on a boolean expression.
Here is an example of how to use ngIf
in a component:
import { Component } from '@angular/core';
@Component({
selector: 'app-user',
template: `
<div *ngIf="user">
<h2>{{ user.name }}</h2>
<p>{{ user.email }}</p>
</div>
<div *ngIf="!user">
No user found.
</div>
`
})
export class UserComponent {
user = { name: 'John Doe', email: 'john@example.com' };
}
In this example, the UserComponent
component defines a user
property that contains an object with the user’s name and email. The *ngIf
directive is used to conditionally display either the user’s information or a message if no user is found.
Q15: How do you create a custom directive in Angular?
A: A directive in Angular is a behavior attached to an element. Here is an example of how to create a custom directive:
import { Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(private el: ElementRef) {
el.nativeElement.style.backgroundColor = 'yellow';
}
}
In this example, the HighlightDirective
directive is created using the @Directive
decorator. The selector
property specifies the CSS selector used to apply the directive to an element. The constructor
method injects the ElementRef
service and uses it to set the background color of the element to yellow.
Recommended: Top Javascript interview questions and answers mostly asked.
Q16: How do you handle events in Angular?
A: Events in Angular are handled using event binding.
Here is an example of how to handle a button click event:
import { Component } from '@angular/core';
@Component({
selector: 'app-button',
template: `
<button (click)="onClick()">Click me</button>
`
})
export class ButtonComponent {
onClick() {
console.log('Button clicked');
}
}
In this example, the ButtonComponent
component defines a button element with an (click)
event binding that calls the onClick()
method when the button is clicked. The onClick()
method logs a message to the console.
Here are some more Angular questions with code examples:
Q17: How do you pass data between components in Angular?
A: Data can be passed between components in Angular using input properties and output events. Here is an example of how to pass data from a parent component to a child component using an input property:
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<app-child [name]="name"></app-child>
`
})
export class ParentComponent {
name = 'John Doe';
}
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<h2>Hello, {{ name }}</h2>
`
})
export class ChildComponent {
@Input() name: string;
}
In this example, the ParentComponent
component defines a name
property and passes it to the ChildComponent
component as an input property using the [name]
syntax. The ChildComponent
component defines an @Input()
decorator on the name
property to indicate that it is an input property.
Q18: How do you handle HTTP requests in Angular?
A: HTTP requests in Angular are handled using the HttpClient
service.
Here is an example of how to make an HTTP GET request:
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-users',
template: `
<ul>
<li *ngFor="let user of users">{{ user.name }}</li>
</ul>
`
})
export class UsersComponent {
users = [];
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get<any[]>('https://jsonplaceholder.typicode.com/users')
.subscribe(users => {
this.users = users;
});
}
}
In this example, the UsersComponent
component injects the HttpClient
service and uses it to make an HTTP GET request to the https://jsonplaceholder.typicode.com/users
API. The subscribe()
method is used to handle the response and update the users
property.
Q19: How do you use routing in Angular?
A: Routing in Angular is used to navigate between different views or components.
Here is an example of how to define routes in an Angular application:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home.component';
import { AboutComponent } from './about.component';
import { ContactComponent } from './contact.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'contact', component: ContactComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
In this example, the AppRoutingModule
module defines three routes for the HomeComponent
, AboutComponent
, and ContactComponent
. The RouterModule.forRoot()
method is used to register the routes and create the Router
service. The RouterModule
is exported so that it can be used by other modules in the application.
Q20: How do you use reactive forms in Angular?
A: Reactive forms in Angular are used to collect user input and validate it.
Here is an example of how to create a reactive form:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-contact',
template: `
<form [formGroup]="contactForm" (ngSubmit)="onSubmit()">
<label>Name:</label>
<input type="text" formControlName="name">
<label>Email:</label>
<input type="email" formControlName="email">
<label>Message:</label>
<textarea formControlName="message"></textarea>
<button type="submit" [disabled]="!contactForm.valid">Submit</button>
</form>
`
})
export class ContactComponent {
contactForm: FormGroup;
constructor(private fb: FormBuilder) {
this.contactForm = this.fb.group({
name: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
message: ['', Validators.required]
});
}
onSubmit() {
console.log(this.contactForm.value);
}
}
In this example, the ContactComponent
component injects the FormBuilder
service and uses it to create a reactive form. The form is defined as a FormGroup
that contains three form controls for the name
, email
, and message
fields. Each form control is defined with initial values and validation rules. The [formGroup]
directive is used to bind the form group to the form element. The formControlName
directive is used to bind each form control to its corresponding input field. The disabled
attribute on the submit button is bound to the valid
property of the form group to prevent the user from submitting an invalid form. The onSubmit()
method is called when the form is submitted, and it logs the form value to the console.
Q21: How do you use ngIf directive in Angular?
A: The ngIf
directive in Angular is used to conditionally render or remove an HTML element based on a given expression. Here’s an example of how to use ngIf
in Angular:
<div *ngIf="showElement">This element will only appear if showElement is true.</div>
In this example, the *ngIf
directive is used to conditionally render the div
element based on the value of the showElement
expression. If showElement
is true, the div
element will be rendered. If showElement
is false, the div
element will be removed from the DOM.
You can also use ngIf
with an else
clause to render different content when the expression is false.
Here’s an example:
<div *ngIf="showElement; else elseBlock">This element will only appear if showElement is true.</div>
<ng-template #elseBlock>
<div>This element will appear when showElement is false.</div>
</ng-template>
In this example, the *ngIf
directive is used to conditionally render the first div
element based on the value of the showElement
expression. If showElement
is true, the first div
element will be rendered. If showElement
is false, the elseBlock
template will be rendered instead, which contains a different div
element. The else
clause is defined using an ng-template
element with a template reference variable (#elseBlock
) that is passed as an argument to the else
property of the *ngIf
directive.
You can also use ngIf
to conditionally render a container element that contains multiple child elements.
Here’s an example:
<div *ngIf="showContainer">
<p>This paragraph will only appear if showContainer is true.</p>
<button>Click me</button>
</div>
In this example, the *ngIf
directive is used to conditionally render the div
element, which contains two child elements (p
and button
). If showContainer
is true, both child elements will be rendered. If showContainer
is false, the entire div
element (and its child elements) will be removed from the DOM.
Q22: How do you use the Angular HttpClient to make HTTP requests?
A: The HttpClient
is an Angular service that makes it easy to send HTTP requests and receive responses. Here’s an example of how to use the HttpClient
service to make a GET request to a RESTful API endpoint:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
private apiUrl = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: HttpClient) {}
getPosts(): Observable<any[]> {
return this.http.get<any[]>(this.apiUrl);
}
}
In this example, the DataService
injects the HttpClient
service and defines a private apiUrl
property that points to a RESTful API endpoint. The getPosts()
method sends a GET request to the apiUrl
endpoint using the http.get()
method of the HttpClient
service, and it returns an observable of type any[]
that represents the response data.
You can also send other types of HTTP requests using the HttpClient
service, such as POST, PUT, DELETE, etc. Here’s an example of how to use the http.post()
method to send a POST request with a payload:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class DataService {
private apiUrl = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: HttpClient) {}
addPost(postData: any): Observable<any> {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post<any>(this.apiUrl, postData, { headers });
}
}
In this example, the addPost()
method sends a POST request to the apiUrl
endpoint with a payload that contains postData
. The http.post()
method of the HttpClient
service is used to send the request, along with an options object that contains a headers
property that sets the Content-Type
header to application/json
. The addPost()
method returns an observable of type any
that represents the response data.
Note that the http.post()
method can also take an optional third argument for configuring the request, such as setting query parameters, setting the request body, etc.
Q23: How do you use routing in Angular?
A: Routing in Angular allows you to map URLs to specific components in your application. The Angular Router
is the main class responsible for handling routing.
Here’s an example of how to define routes in your Angular application:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home.component';
import { AboutComponent } from './about.component';
import { ContactComponent } from './contact.component';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'contact', component: ContactComponent },
{ path: '**', redirectTo: '' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
In this example, the AppRoutingModule
defines an array of Routes
that maps URLs to specific components. Each route object has a path
property that defines the URL path that will activate the component, and a component
property that specifies the component that will be rendered. The first route object maps the empty path to the HomeComponent
. The second and third route objects map the /about
and /contact
paths to the AboutComponent
and ContactComponent
, respectively. The last route object uses a wildcard **
path to redirect any unknown paths back to the home path.
To use the routes defined in the AppRoutingModule
, you need to import the RouterModule
and call the forRoot()
method to create a new router configuration with your defined routes. Here’s an example of how to use routing in your app component:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<nav>
<a routerLink="">Home</a>
<a routerLink="/about">About</a>
<a routerLink="/contact">Contact</a>
</nav>
<router-outlet></router-outlet>
`
})
export class AppComponent {}
In this example, the AppComponent
template includes a nav
element with three links that use the routerLink
directive to navigate to the corresponding paths defined in the AppRoutingModule
. The router-outlet
directive is used to render the component that corresponds to the current route. When a user clicks on one of the links, the Router
service will navigate to the corresponding URL and render the corresponding component.
Q24: How do you use observables in Angular?
A: Observables are a powerful feature of Angular that allow you to handle asynchronous data streams. Angular provides the Observable
class from the rxjs
library to work with observables.
Here’s an example of how to use observables in Angular:
import { Component } from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
template: `
<button (click)="getData()">Get Data</button>
<ul>
<li *ngFor="let item of data$ | async">{{ item }}</li>
</ul>
`
})
export class AppComponent {
data$: Observable<string[]>;
getData() {
this.data$ = new Observable(observer => {
setTimeout(() => {
observer.next(['apple', 'banana', 'cherry']);
observer.complete();
}, 1000);
});
}
}
In this example, the AppComponent
template includes a button with a click event that triggers the getData()
method. The data$
property is an observable of type string[]
. The getData()
method creates a new observable that simulates an asynchronous data stream using the setTimeout()
function. The observable calls the observer.next()
method to emit a value (an array of fruits) and the observer.complete()
method to signal the end of the stream.
The async
pipe is used to subscribe to the data$
observable in the template. When the observable emits a value, the async
pipe automatically updates the view with the new data.
You can also use the subscribe()
method of an observable to manually handle the emitted values. Here’s an example of how to use the subscribe()
method to log the emitted values:
import { Component } from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
template: `
<button (click)="getData()">Get Data</button>
`
})
export class AppComponent {
data$: Observable<string[]>;
getData() {
this.data$ = new Observable(observer => {
setTimeout(() => {
observer.next(['apple', 'banana', 'cherry']);
observer.complete();
}, 1000);
});
this.data$.subscribe({
next: value => console.log(value),
error: error => console.error(error),
complete: () => console.log('Complete')
});
}
}
In this example, the getData()
method creates an observable that emits an array of fruits after a 1-second delay. The subscribe()
method is called to manually handle the emitted values. The next()
method is called with the emitted value (an array of fruits), the error()
method is called if there is an error in the stream, and the complete()
method is called when the stream ends. In this example, the next()
method logs the emitted value to the console, and the complete()
method logs a completion message.
Q25: How do you use ngIf in Angular?
A: ngIf
is a built-in structural directive in Angular that allows you to conditionally render or remove an element from the DOM based on an expression. Here’s an example of how to use ngIf
in Angular:
<div *ngIf="showElement">
<p>This element will only be displayed if showElement is true.</p>
</div>
In this example, the div
element is conditionally rendered based on the showElement
property. If showElement
is true, the div
element and its contents will be displayed in the DOM. If showElement
is false, the div
element will be removed from the DOM.
You can also use ngIf
with an else
clause to conditionally render an alternate element if the expression evaluates to false.
Here’s an example:
<div *ngIf="showElement; else noElement">
<p>This element will only be displayed if showElement is true.</p>
</div>
<ng-template #noElement>
<p>This element will be displayed if showElement is false.</p>
</ng-template>
In this example, the div
element is conditionally rendered based on the showElement
property. If showElement
is true, the div
element and its contents will be displayed in the DOM. If showElement
is false, the ng-template
with the #noElement
reference will be rendered instead.
You can also use ngIf
with an expression that evaluates to a truthy or falsy value.
Here’s an example:
<div *ngIf="fruits.length">
<p>This element will only be displayed if the fruits array has at least one element.</p>
</div>
In this example, the div
element is conditionally rendered based on the fruits
array. If the fruits
array has at least one element, the div
element and its contents will be displayed in the DOM. If the fruits
array is empty, the div
element will be removed from the DOM.
Q26: How do you pass data between components in Angular?
A: There are several ways to pass data between components in Angular, depending on the relationship between the components and the type of data being passed. Here are some of the most common methods:
1. Input and output properties:
If the components have a parent-child relationship, you can pass data from the parent component to the child component using input properties, and from the child component to the parent component using output properties. Here’s an example:
Parent component template:
<app-child [message]="parentMessage" (childEvent)="onChildEvent($event)"></app-child>
Parent component class:
export class ParentComponent {
parentMessage = 'Message from parent';
onChildEvent(event: any) {
console.log(event);
}
}
Child component class:
export class ChildComponent {
@Input() message: string;
@Output() childEvent = new EventEmitter<string>();
sendMessage() {
this.childEvent.emit('Message from child');
}
}
In this example, the parent component passes the parentMessage
property to the child component using the input property [message]
, and listens to the childEvent
output property using the (childEvent)
event binding. The child component receives the message
input property using the @Input()
decorator, and emits the childEvent
output property using the EventEmitter
.
2. Service:
If the components have a non-parent-child relationship, you can use a shared service to pass data between them. The shared service can have properties and methods that store and retrieve data, and the components can inject the service and use its methods to pass data. Here’s an example:
Shared service class:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class DataService { private messageSource = new BehaviorSubject<string>('Default message');
currentMessage = this.messageSource.asObservable();
changeMessage(message: string) {
this.messageSource.next(message);
}
}
Sender component class:
import { Component } from '@angular/core';
import { DataService } from '../data.service';
@Component({
selector: 'app-sender',
template: `
<input type="text" [(ngModel)]="message">
<button (click)="sendMessage()">Send Message</button> `,
styleUrls: ['./sender.component.css'] })
export class SenderComponent { message: string; constructor(private dataService: DataService) { }
sendMessage() { this.dataService.changeMessage(this.message); } }
Receiver component class:
import { Component, OnInit } from "@angular/core";
import { DataService } from "../data.service";
@Component({
selector: "app-receiver",
template: ` <p>{{ message }}</p> `,
styleUrls: ["./receiver.component.css"],
})
export class ReceiverComponent implements OnInit {
message: string;
constructor(private dataService: DataService) {}
ngOnInit() {
this.dataService.currentMessage.subscribe(
(message) => (this.message = message)
);
}
}
In this example, the DataService
has a currentMessage
property that is an observable of a BehaviorSubject
, and a changeMessage()
method that updates the BehaviorSubject
with a new message. The sender component uses the DataService
to call the changeMessage()
method and pass the new message. Receiver component uses the DataService
to subscribe to the currentMessage
property and get the latest message.
3. Route parameters:
If you need to pass data between components based on a route change, you can use route parameters to pass data in the URL. Here’s an example:
App routing module:
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { HomeComponent } from "./home/home.component";
import { UserComponent } from "./user/user.component";
const routes: Routes = [
{ path: "", component: HomeComponent },
{ path: "user/:id", component: UserComponent },
];
@NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] })
export class AppRoutingModule {}
Home component template:
<a [routerLink]="['/user', 1]">User 1</a> <a [routerLink]="['/user', 2]">User 2</a>
User component class:
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
@Component({
selector: "app-user",
template: ` <p>User {{ id }} profile</p> `,
styleUrls: ["./user.component.css"],
})
export class UserComponent implements OnInit {
id: number;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.id = parseInt(this.route.snapshot.paramMap.get("id"));
}
}
In this example, the AppRoutingModule
defines a route with a parameter id
. The Home component uses routerLink
to navigate to the User component with a specific id
. The User component uses ActivatedRoute
to get the id
parameter from the URL and use it in the component.
These are just a few examples of how to pass data between components in Angular. Other methods include using ViewChild, ng-content, or event bus. The choice of method depends on the specific requirements and relationship between the components.
4. Local storage:
If you need to share data between components that persist across sessions, you can use the browser’s local storage to store and retrieve data. Here’s an example:
DataService class:
import { Injectable } from "@angular/core";
const STORAGE_KEY = "my_data";
@Injectable({ providedIn: "root" })
export class DataService {
private data: any[];
constructor() {
this.data = JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
}
getData() {
return this.data;
}
setData(data: any[]) {
this.data = data;
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
}
}
Component classes:
import { Component, OnInit } from "@angular/core";
import { DataService } from "../data.service";
@Component({
selector: "app-component-a",
template: `
<p>{{ data }}</p>
<button (click)="changeData()">Change data</button>
`,
styleUrls: ["./component-a.component.css"],
})
export class ComponentAComponent implements OnInit {
data: any[];
constructor(private dataService: DataService) {}
ngOnInit() {
this.data = this.dataService.getData();
}
changeData() {
this.data[0].value += 1;
this.dataService.setData(this.data);
}
}
@Component({
selector: "app-component-b",
template: ` <p>{{ data }}</p> `,
styleUrls: ["./component-b.component.css"],
})
export class ComponentBComponent implements OnInit {
data: any[];
constructor(private dataService: DataService) {}
ngOnInit() {
this.data = this.dataService.getData();
}
}
In this example, the DataService
uses the browser’s local storage to store and retrieve data. Component A and Component B both inject the DataService
and use it to get and set the same data. When Component A changes the data, it calls setData()
in the DataService
to persist the change. Component B can then retrieve the updated data using getData()
.
5. Input properties:
If you need to pass data from a parent component to a child component, you can use input properties. Here’s an example:
Parent component template:
<app-child [data]="myData"></app-child>
Child component class:
import { Component, Input } from "@angular/core";
@Component({
selector: "app-child",
template: ` <p>{{ data }}</p> `,
styleUrls: ["./child.component.css"],
})
export class ChildComponent {
@Input() data: any;
}
In this example, the parent component passes its myData
property to the child component using [data]="myData"
syntax. The child component declares an @Input() data
property to receive the data from the parent component. The child component can then use the data
property in its template.
6. Output properties and event emitters:
If you need to pass data from a child component to a parent component, you can use output properties and event emitters. Here’s an example:
Child component class:
import { Component, Output, EventEmitter } from "@angular/core";
@Component({
selector: "app-child",
template: ` <button (click)="sendData()">Send data to parent</button> `,
styleUrls: ["./child.component.css"],
})
export class ChildComponent {
@Output() dataEvent = new EventEmitter<string>();
sendData() {
const data = "Hello from child";
this.dataEvent.emit(data);
}
}
Parent component template:
<app-child (dataEvent)="handleData($event)"></app-child>
<p>{{ receivedData }}</p>
Parent component class:
import { Component } from "@angular/core";
@Component({
selector: "app-parent",
template: `
<app-child (dataEvent)="handleData($event)"></app-child>
<p>{{ receivedData }}</p>
`,
styleUrls: ["./parent.component.css"],
})
export class ParentComponent {
receivedData: string;
handleData(data: string) {
this.receivedData = data;
}
}
In this example, the child component declares an @Output() dataEvent
property of type EventEmitter
. When the child component’s button is clicked, it emits an event using this.dataEvent.emit(data)
, where data
is the string “Hello from child”. The parent component binds to the dataEvent
event using (dataEvent)="handleData($event)"
syntax. When the event is emitted by the child component, the parent component’s handleData()
method is called with the emitted data as an argument. The parent component then updates its receivedData
property, which is displayed in the template.
7. NgIf directive:
If you need to conditionally show or hide a part of the template based on a boolean expression, you can use the *ngIf
directive. Here’s an example:
Component class:
import { Component } from "@angular/core";
@Component({
selector: "app-example",
template: `
<button (click)="toggle()">Toggle text</button>
<p *ngIf="showText">This text is shown when showText is true</p>
`,
styleUrls: ["./example.component.css"],
})
export class ExampleComponent {
showText = true;
toggle() {
this.showText = !this.showText;
}
}
In this example, the ExampleComponent
class has a showText
boolean property that determines whether to show or hide the text. The component’s template uses the *ngIf="showText"
syntax to conditionally show or hide the p
element based on the showText
property. When the button is clicked, the toggle()
method is called, which toggles the value of showText
.
8. NgFor directive:
If you need to loop through an array and render a template for each item in the array, you can use the *ngFor
directive. Here’s an example:
Component class:
import { Component } from "@angular/core";
@Component({
selector: "app-example",
template: `
<h2>List of items</h2>
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>
`,
styleUrls: ["./example.component.css"],
})
export class ExampleComponent {
items = ["Item 1", "Item 2", "Item 3"];
}
In this example, the ExampleComponent
class has an items
array property that contains three items. The component’s template uses the *ngFor
directive to loop through the items
array and render an li
element for each item.
9. Template reference variables:
If you need to access a DOM element or a child component instance in the template, you can use template reference variables. Here’s an example:
Component class:
import { Component } from "@angular/core";
@Component({
selector: "app-example",
template: `
<input #nameInput type="text" placeholder="Enter your name" />
<button (click)="submitName(nameInput.value)">Submit</button>
`,
styleUrls: ["./example.component.css"],
})
export class ExampleComponent {
submitName(name: string) {
alert(`Hello, ${name}!`);
}
}
In this example, the component’s template has an input
element with a #nameInput
template reference variable. The submitName()
method is called when the button is clicked, and it accesses the value
property of the nameInput
template reference variable to get the value entered by the user.
10. Dependency injection:
Angular has a powerful dependency injection system that allows you to inject dependencies into components and services. Here’s an example:
Service class:
import { Injectable } from "@angular/core";
@Injectable({ providedIn: "root" })
export class ExampleService {
sayHello() {
console.log("Hello from ExampleService!");
}
}
Component class:
import { Component } from "@angular/core";
import { ExampleService } from "./example.service";
@Component({
selector: "app-example",
template: ` <button (click)="sayHello()">Say hello</button> `,
styleUrls: ["./example.component.css"],
})
export class ExampleComponent {
constructor(private exampleService: ExampleService) {}
sayHello() {
this.exampleService.sayHello();
}
}
In this example, the ExampleService
class is a simple service that has a sayHello()
method that logs a message to the console. The service is decorated with the @Injectable({ providedIn: 'root' })
decorator, which means that it is provided at the root level and can be injected into any component or service in the application. The ExampleComponent
class has a constructor that injects an instance of the ExampleService
into the exampleService
private property. When the button is clicked, the sayHello()
method is called, which in turn calls the sayHello()
method of the exampleService
.
Q27: What is the difference between a module and a component in Angular?
A: A module is a container for a group of related components, directives, services, and pipes. It defines an Angular application or a part of an application. It has a @NgModule
decorator and is typically defined in a separate file. A module can import other modules and export components, directives, services, and pipes that can be used by other modules.
A component is a building block of an Angular application that represents a part of the UI. It has a @Component
decorator and is typically defined in a separate file. A component defines a template that describes the structure and appearance of the UI and a class that provides the behavior and data for the template.
Q28: What is data binding in Angular?
A: Data binding is a way to connect the data in a component’s class with the template that is displayed to the user. There are four types of data binding in Angular:
Interpolation ({{ }}
) binding: This is a one-way binding that binds a component’s property value to a template expression. It is used to display the value of a property in the template.
Property binding: This is a one-way binding that binds a component’s property to an HTML attribute, property, or directive. It is used to set the value of a property in the template.
Event binding: This is a one-way binding that binds an HTML event to a method in the component’s class. It is used to respond to user events, such as clicks or key presses.
Two-way binding: This is a two-way binding that combines a property binding and an event binding to allow data to flow both ways between the component and the template. It is used to create interactive UI elements, such as input fields.
Q29: What is a service in Angular?
A: A service is a class that provides a specific functionality that can be shared across multiple components. It is used to separate business logic from presentation logic and to promote reusability and maintainability. A service can be injected into a component’s constructor using Angular’s dependency injection system.
Q30: What is Angular CLI?
A: Angular CLI (Command Line Interface) is a command-line tool that helps you create, configure, and maintain Angular projects. It provides a set of commands for generating components, services, modules, and other Angular artifacts, as well as for building, testing, and deploying your application.
Q31: What is a directive in Angular?
A: A directive is a class that adds behavior to an existing DOM element or component. It can be used to manipulate the DOM, add or remove attributes, listen to events, or create custom structural or attribute directives. There are three types of directives in Angular:
Component directive: This is a directive that creates a new custom element that can be used in the HTML.
Attribute directive: This is a directive that adds or removes an attribute from an existing HTML element.
Structural directive: This is a directive that adds or removes DOM elements based on a condition or a loop. Examples of structural directives include *ngIf
and *ngFor
.
Q32: What is Angular router and how does it work?
A: Angular router is a module that provides a way to navigate between different views or components in an Angular application. It maps URLs to components and provides a way to pass data between them. The router works by defining a set of routes that map URLs to components, and then using a router outlet to display the component based on the current URL.
Here’s an example:
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 {}
In this example, we define two routes: one for the home component and one for the about component. The path
property specifies the URL path that maps to the component, and the component
property specifies the component that should be displayed when the path is matched. The AppRoutingModule
module imports the RouterModule.forRoot()
method and passes in the routes, and then exports the RouterModule
module for use in other modules.
Q33. What is dependency injection in Angular and how does it work?
A: Dependency injection (DI) is a design pattern that is used to manage the dependencies between objects in an application. In Angular, DI is used to provide an instance of a service or a component to another object that needs it. The Angular DI system is hierarchical, which means that a provider can be defined at a higher level and then inherited by child components.
Here’s an example:
import { Component } from "@angular/core";
import { DataService } from "./data.service";
@Component({
selector: "app-root",
template: `
<h1>{{ title }}</h1>
<p>{{ message }}</p>
`,
})
export class AppComponent {
title = "My App";
message: string;
constructor(private dataService: DataService) {}
ngOnInit() {
this.message = this.dataService.getMessage();
}
}
In this example, we define an AppComponent
class that has a dependency on the DataService
service. We inject the service into the component’s constructor using the private
keyword, which tells Angular to create an instance of the service and provide it to the component. In the ngOnInit()
method, we call the service’s getMessage()
method to retrieve some data and assign it to the message
property.
Q34: What is ng-container in Angular and what is it used for?
A: ng-container
is a structural directive in Angular that allows you to group elements together without adding an extra element to the DOM. It is often used in combination with other structural directives such as ngIf
and ngFor
. Here’s an example:
import { Component } from "@angular/core";
@Component({
selector: "app-root",
template: `
<ng-container *ngIf="showMessage">
<h1>Hello, {{ name }}!</h1>
<p>How are you today?</p>
</ng-container>
`,
})
export class AppComponent {
name = "John";
showMessage = true;
}
In this example, we use ng-container
with the ngIf
directive to conditionally render some HTML based on the value of the showMessage
property. The ng-container
directive doesn’t add any extra HTML to the DOM, so the rendered output will only contain the h1
and p
elements if showMessage
is true.
Q35: What is NgZone in Angular and what is it used for?
A: NgZone
is a class in Angular that provides a way to execute code outside or inside of the Angular zone. The Angular zone is a concept in Angular that defines the execution context for an application. The zone is responsible for detecting changes to the application’s state and triggering a change detection cycle. The NgZone
class provides a way to execute code outside of the zone, which can be useful for improving performance or working with third-party libraries that don’t use the Angular zone.
Here’s an example:
import { Component, NgZone } from "@angular/core";
@Component({
selector: "app-root",
template: `
<p>{{ message }}</p>
<button (click)="updateMessage()">Update Message</button>
`,
})
export class AppComponent {
message = "Hello, World!";
constructor(private zone: NgZone) {}
updateMessage() {
this.zone.runOutsideAngular(() => {
setTimeout(() => {
this.message = "Hello, Universe!";
this.zone.run(() => {});
}, 1000);
});
}
}
In this example, we use the NgZone
class to execute the setTimeout()
function outside of the Angular zone using the runOutsideAngular()
method. This can help improve performance because the change detection cycle won’t be triggered by the setTimeout()
function. We then use the run()
method to run the change detection cycle after the setTimeout()
function has completed.
Q36: What is a service in Angular and how is it used?
A: A service is a class in Angular that provides a way to share data and functionality between components. Services can be injected into components, directives, pipes, and other services using dependency injection. Services can be used to perform tasks such as fetching data from a server, caching data, or logging messages to the console.
Here’s an example of a simple service:
import { Injectable } from "@angular/core";
@Injectable({ providedIn: "root" })
export class MessageService {
messages: string[] = [];
add(message: string) {
this.messages.push(message);
}
clear() {
this.messages = [];
}
}
In this example, we define a MessageService
class that provides an array of messages that can be added to or cleared. The @Injectable()
decorator is used to indicate that this class can be injected with dependencies. We then define two methods, add()
and clear()
, that modify the array of messages. This service can be injected into a component using the following code:
import { Component } from "@angular/core";
import { MessageService } from "./message.service";
@Component({
selector: "app-root",
template: `
<h1>{{ title }}</h1>
<button (click)="addMessage()">Add Message</button>
<button (click)="clearMessages()">Clear Messages</button>
<ul>
<li *ngFor="let message of messageService.messages">{{ message }}</li>
</ul>
`,
})
export class AppComponent {
title = "My App";
constructor(public messageService: MessageService) {}
addMessage() {
this.messageService.add("Hello, World!");
}
clearMessages() {
this.messageService.clear();
}
}
In this example, we inject the MessageService
into the AppComponent
constructor and then use the add()
and clear()
methods to modify the message array. We also use the *ngFor
directive to loop through the messages and display them in an unordered list.
Q37: What is the difference between declarations, providers, and imports in Angular modules?
A: In Angular, modules are used to group related components, directives, services, and other objects together. When defining an Angular module, there are three main properties that can be set: declarations
, providers
, and imports
.
declarations
: This property is used to declare the components, directives, and pipes that belong to the module. Any component, directive, or pipe that is used in the module must be declared here.providers
: This property is used to provide services that are used by the module. Services can be provided at the module level or at the component level.imports
: This property is used to import other modules into the current module. This allows the current module to use components, directives, pipes, and services that are defined in other modules.
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { AppComponent } from "./app.component";
@NgModule({
declarations: [AppComponent],
providers: [],
imports: [BrowserModule],
bootstrap: [AppComponent],
})
export class AppModule {}
In this example, we import the BrowserModule
from the @angular/platform-browser
package and declare the AppComponent
in the declarations
property. We don’t provide any services in this module, so the providers
property is an empty array. Finally, we import the BrowserModule
in the imports
property and set the AppComponent
as the bootstrap component for the module.
Q38: What is the purpose of the async pipe in Angular?
A: The async pipe in Angular is used to subscribe to an Observable or Promise and display its value in the template. When the Observable or Promise resolves, the async pipe updates the view with the new value. The async pipe also automatically unsubscribes from the Observable or Promise when the component is destroyed, preventing memory leaks.
Here’s an example of using the async pipe to display the result of an Observable in a template:
import { Component } from "@angular/core";
import { Observable } from "rxjs";
import { DataService } from "./data.service";
@Component({
selector: "app-root",
template: `
<h1>{{ title }}</h1>
<ul>
<li *ngFor="let item of items$ | async">{{ item }}</li>
</ul>
`,
})
export class AppComponent {
title = "My App";
items$: Observable<string[]>;
constructor(private dataService: DataService) {
this.items$ = this.dataService.getItems();
}
}
In this example, we inject a DataService
dependency into the AppComponent
constructor and assign the result of the getItems()
method to the items$
property. We then use the *ngFor
directive to loop through the items and display them in an unordered list. The async
pipe is used to subscribe to the items$
Observable and update the view when it emits a new value.
Q39: What is the purpose of the router outlet in Angular?
A: The router outlet in Angular is a directive that is used to display the components associated with a particular route. When a user navigates to a specific URL in an Angular application, the router determines which component to display based on the current route configuration. The router then inserts the component into the router outlet, replacing any existing component that was previously displayed.
Here’s an example of using the router outlet to display the components associated with two different routes:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } 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 { }
In this example, we define two routes: the empty path, which maps to the HomeComponent
, and the /about
path, which maps to the AboutComponent
. We then import the RouterModule
and call its forRoot()
method with the routes
array to configure the router. Finally, we export the RouterModule
so it can be used in other modules.
Here’s an example of using the router outlet in a template:
<!-- app.component.html -->
<header>
<nav>
<a routerLink="/">Home</a>
<a routerLink="/about">About</a>
</nav>
</header>
<main>
<router-outlet></router-outlet>
</main>
In this example, we define a navigation menu with links to the home and about routes. We then use the router outlet directive to display the component associated with the current route. When the user clicks on a link in the navigation menu, the router will navigate to the corresponding route and insert the associated component into the router outlet.
Here is another Angular question and answer with code example:
Q40: How do you communicate between components in Angular?
A: There are several ways to communicate between components in Angular, including using input and output properties, services, and event emitters.
One way to communicate between a parent and child component is to use input and output properties. The parent component can pass data down to the child component through an input property, and the child component can emit events through an output property to communicate with the parent component. Here’s an example:
// parent.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<h2>Parent Component</h2>
<app-child [message]="message" (messageEvent)="receiveMessage($event)"></app-child>
`,
})
export class ParentComponent {
message = 'Hello from parent';
receiveMessage($event) {
this.message = $event;
}
}
// child.component.ts
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<h2>Child Component</h2>
<p>{{ message }}</p>
<button (click)="sendMessage()">Send Message</button>
`,
})
export class ChildComponent {
@Input() message: string;
@Output() messageEvent = new EventEmitter<string>();
sendMessage() {
this.messageEvent.emit('Hello from child');
}
}
In this example, the parent component passes a message to the child component through the message
input property. The child component displays the message and emits an event when the user clicks the “Send Message” button. The parent component listens for the event through the (messageEvent)
output property and updates the message with the received data.
Another way to communicate between components is to use a service. A service is a singleton object that can be injected into multiple components and used to share data and functionality between them. Here’s an example:
// message.service.ts
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class MessageService {
private messageSource = new Subject<string>();
message$ = this.messageSource.asObservable();
sendMessage(message: string) {
this.messageSource.next(message);
}
}
// parent.component.ts
import { Component } from '@angular/core';
import { MessageService } from './message.service';
@Component({
selector: 'app-parent',
template: `
<h2>Parent Component</h2>
<button (click)="sendMessage()">Send Message</button>
`,
})
export class ParentComponent {
constructor(private messageService: MessageService) {}
sendMessage() {
this.messageService.sendMessage('Hello from parent');
}
}
// child.component.ts
import { Component, OnInit } from '@angular/core';
import { MessageService } from './message.service';
@Component({
selector: 'app-child',
template: `
<h2>Child Component</h2>
<p>{{ message }}</p>
`,
})
export class ChildComponent implements OnInit {
message: string;
constructor(private messageService: MessageService) {}
ngOnInit() {
this.messageService.message$.subscribe((message) => (this.message = message));
}
}
In this example, we define a MessageService
that has a messageSource
subject and a sendMessage()
method to emit new messages. The message$
property exposes the subject as an observable that can be subscribed to by components. The parent component injects the MessageService
and calls the sendMessage
method to send a message. The child component also injects the MessageService
and subscribes to the message$
observable to receive new messages.
A third way to communicate between components is to use an event emitter. An event emitter is an Angular class that can be used to emit custom events that can be subscribed to by other components. Here’s an example:
// message-event.ts
import { EventEmitter } from '@angular/core';
export class MessageEvent {
private static instance: MessageEvent;
private emitter = new EventEmitter<string>();
private constructor() {}
static getInstance(): MessageEvent {
if (!MessageEvent.instance) {
MessageEvent.instance = new MessageEvent();
}
return MessageEvent.instance;
}
emit(message: string) {
this.emitter.emit(message);
}
subscribe(next?: (value: string) => void, error?: (error: any) => void, complete?: () => void) {
return this.emitter.subscribe(next, error, complete);
}
}
// parent.component.ts
import { Component } from '@angular/core';
import { MessageEvent } from './message-event';
@Component({
selector: 'app-parent',
template: `
<h2>Parent Component</h2>
<button (click)="sendMessage()">Send Message</button>
`,
})
export class ParentComponent {
private messageEvent = MessageEvent.getInstance();
sendMessage() {
this.messageEvent.emit('Hello from parent');
}
}
// child.component.ts
import { Component, OnInit } from '@angular/core';
import { MessageEvent } from './message-event';
@Component({
selector: 'app-child',
template: `
<h2>Child Component</h2>
<p>{{ message }}</p>
`,
})
export class ChildComponent implements OnInit {
private messageEvent = MessageEvent.getInstance();
message: string;
ngOnInit() {
this.messageEvent.subscribe((message) => (this.message = message));
}
}
In this example, we define a MessageEvent
class that has a static getInstance()
method to create a singleton instance of the event emitter. The emit()
method can be called to emit new messages, and the subscribe()
method can be called to subscribe to the event and receive new messages. The parent and child components both access the same MessageEvent
instance and can communicate by emitting and subscribing to events.
These are just a few examples of how you can communicate between components in Angular. The approach you choose will depend on your specific use case and the complexity of your application.
Q41. What is an Angular pipe and how does it work?
A: An Angular pipe is a way to transform data before it is displayed in the template. Pipes are used to format data, filter data, or transform data in some other way. Angular comes with several built-in pipes, such as DatePipe
, UpperCasePipe
, and DecimalPipe
, and you can also create custom pipes.
Here’s an example:
import { Component } from "@angular/core";
@Component({
selector: "app-root",
template: `
<h1>{{ title | uppercase }}</h1>
<p>{{ message | slice : 0 : 20 }}</p>
`,
})
export class AppComponent {
title = "My App";
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
}
In this example, we use two built-in pipes: UpperCasePipe
and SlicePipe
. The UpperCasePipe
transforms the title
string to uppercase, and the SlicePipe
extracts a portion of the message
string from the first character to the 20th character.
Q42: What is dependency injection in Angular and how does it work?
A: Dependency injection is a design pattern in Angular that provides a way to create objects that depend on other objects. In Angular, dependency injection is used to provide components, services, and other objects with the dependencies they need to function. Instead of creating dependencies inside a component or service, dependencies are provided to the component or service through the constructor.
Here’s an example:
import { Component } from "@angular/core";
import { UserService } from "./user.service";
@Component({
selector: "app-root",
template: `
<h1>{{ title }}</h1>
<p>Welcome, {{ username }}!</p>
`,
})
export class AppComponent {
title = "My App";
username = "";
constructor(private userService: UserService) {
this.username = this.userService.getUsername();
}
}
In this example, we inject a UserService
dependency into the AppComponent
constructor using the private
access modifier. The getUserame()
method on the UserService
class returns the current username, which we then assign to the username
property of the AppComponent
class. Dependency injection makes it easy to provide components and services with the dependencies they need, and it promotes code reusability and maintainability.