Observables in Angular

 Observables in Angular


Observables are used in Angular to handle asynchronous operations, such as HTTP requests. Here is an example of an observable in Angular:


import { Observable } from 'rxjs';

import { HttpClient } from '@angular/common/http';


@Injectable({

  providedIn: 'root',

})

export class UserService {

  private apiUrl = 'https://api.example.com/users';


  constructor(private http: HttpClient) {}


  getUsers(): Observable<any> {

    return this.http.get<any>(this.apiUrl);

  }

}

This code sets up an observable to get a list of users from an API. The getUsers function returns an observable that emits an array of user objects.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...