Angular Interview questions and answers
how to implement Observables, Promises, Lazy Loading, and Eager Loading in Angular:
1. Observable in Angular
Definition: An Observable is used for handling asynchronous data streams where multiple values can be emitted over time.
Implementation:
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
@Component({
selector: 'app-observable-demo',
template: `<div>{{ data | async }}</div>`,
})
export class ObservableDemoComponent implements OnInit {
data: Observable<string>;
ngOnInit() {
this.data = new Observable((observer) => {
setTimeout(() => observer.next('Observable Data 1'), 1000);
setTimeout(() => observer.next('Observable Data 2'), 2000);
setTimeout(() => observer.complete(), 3000);
});
}
}
In this example, the data observable emits values over time, and the | async pipe in the template automatically subscribes to the observable.
2. Promise in Angular
Definition: A Promise handles asynchronous operations but resolves only once, providing either a success value or an error.
Implementation:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-promise-demo',
template: `<div>{{ message }}</div>`,
})
export class PromiseDemoComponent implements OnInit {
message: string;
ngOnInit() {
this.getData().then((res) => {
this.message = res;
}).catch((err) => {
this.message = err;
});
}
getData(): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => resolve('Promise Data Resolved'), 2000);
});
}
}
In this example, the getData() method returns a promise that resolves after 2 seconds.
3. Lazy Loading in Angular
Definition: Lazy loading is the technique of loading feature modules only when they are needed (usually upon navigation).
Steps to Implement Lazy Loading:
1. Create a feature module (e.g., feature.module.ts).
2. Configure routes with loadChildren.
Example:
// app-routing.module.ts
const routes: Routes = [
{ path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
In the feature.module.ts:
@NgModule({
declarations: [FeatureComponent],
imports: [CommonModule, RouterModule.forChild([{ path: '', component: FeatureComponent }])]
})
export class FeatureModule { }
Here, the FeatureModule is loaded only when the user navigates to /feature.
4. Eager Loading in Angular
Definition: Eager loading is the default loading strategy where modules are loaded as soon as the application starts.
Steps to Implement Eager Loading:
Simply declare the module in the app.module.ts imports array, without the loadChildren property.
Example:
// app.module.ts
import { FeatureModule } from './feature/feature.module';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, FeatureModule],
bootstrap: [AppComponent]
})
export class AppModule { }
Here, the FeatureModule is eagerly loaded when the application starts.
Key Differences Between Lazy Loading and Eager Loading
1. Lazy Loading: Modules are loaded when they are required, reducing the initial load time of the application.
2. Eager Loading: Modules are loaded at the application startup, increasing the initial load time but making the module instantly available.
#dotnettechpro #dotnet #csharp #angular
These examples cover how to implement observables, promises, lazy loading, and eager loading in Angular. Let me know if you need further clarification!
Comments
Post a Comment