There are so many questions and doubts regarding Observable in Angular we will cover the main topic step by step.
Here is a more basic question is What is an Observable we will understand in a more basic and brief way.
Observable: Observables are used to move data into an Application, One component to another component, and Client to server and server to the client.
This is the most basic and easy to understand the definition of Observables.
now your concept about observable might be clear. now let’s understand with specific Definitions.
Angular makes use of observables as an interface to handle a variety of common asynchronous operations. For example:
The observable behavior follows the programming pattern of an observer which has two main players:
The Observable sends data while the Observer subscribes to it to receive the data.
The Observable fires the data in response to an event. For example, when a user clicks a button or in response to data that is received from a remote server.
On the other hand, the Observer has three handles to use the data that it receives:
now will understand with the example, how can we retrieve data from the server using Observables.
Consider the following example for reference:
import { Component, OnInit } from '@angular/core'; import { RestaurentService } from 'src/app/services/restaurent.service'; @Component({ selector: 'app-restaurent-list', templateUrl: './restaurent-list.component.html', styleUrls: ['./restaurent-list.component.css'], }) export class RestaurentListComponent implements OnInit { restaurentList: any []=[]; constructor(private restaurentService: RestaurentService) {} ngOnInit(): void { this.readRestaurent(); } readRestaurent(): void { this.restaurentService.readAll().subscribe(restaurentdata => { this.restaurentList = JSON.parse(restaurentdata.Data) }, error => { console.log("Error while getting posts ", error); }); } }
now we need to create services to perform some action here is the example of a service component to retrieve data from a server using Observables and HTTP client.
to connect with the server we need basic requirements like base URL and API EndPoint.
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable} from 'rxjs'; const baseURL = 'http://localhost:4400/api/' @Injectable({ providedIn: 'root' }) export class RestaurentService { constructor(private httpClient: HttpClient) { } readAll(): Observable<any>{ return this.httpClient.get(baseURL+'Restaurent/getrestaurentlist'); } }
This is the Basic Example of the Observables and using this we can perform many actions as per our requirements.
Finally, now with this article, your basic concept of Observable might be clear. and also with help of the given example, you can start your first step into observables. observables are very helpful and interesting in their own way we will discuss further topic later.
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular