English 中文(简体)
RxJS - Working with RxJS & Angular
  • 时间:2024-10-18

Working with RxJS & Angular


Previous Page Next Page  

In this chapter, we will see how to use RxJs with Angular. We will not get into the installation process for Angular here, to know about Angular Installation refer this pnk −https://www.tutorialspoint.com/angular7/angular7_environment_setup.htm

We will directly work on an example, where will use Ajax from RxJS to load data.

Example

app.component.ts


import { Component } from  @angular/core ;
import { environment } from  ./../environments/environment ;
import { ajax } from  rxjs/ajax ;
import { map } from  rxjs/operators 

@Component({
   selector:  app-root ,
   templateUrl:  ./app.component.html ,
   styleUrls: [ ./app.component.css ]
})
export class AppComponent {
   title =   ;
   data;
   constructor() {
      this.data = "";
      this.title = "Using RxJs with Angular";
      let a = this.getData();
   }
   getData() {
      const response =
      ajax( https://jsonplaceholder.typicode.com/users )
         .pipe(map(e => e.response));
      response.subscribe(res => {
         console.log(res);
         this.data = res;
      });
   }
}

app.component.html


<span>
   <h3>{{title}}</h3>
   <ul *ngFor="let i of data">
      <p>{{i.id}}: {{i.name}}</p>
   </ul>
</span>

<router-outlet></router-outlet>

We have used ajax from RxJS that will load data from this url − https://jsonplaceholder.typicode.com/users.

When you compile the display is as shown below −

RxJs with Angular Advertisements