Sending multiple requests when using ag-grid and angular 2

I am fetching data from ASP.NET Webapi and populating an ag-grid in angular 2 app.

The data is being populated in the ag-grid, but I noticed that the requests on the developer tool web tab are flooded with requests and eventually get timeout. I don't know why this is happening. It looks like the web api is called endlessly.

Here's the angular code:

EVENT-details.component.ts

import { IEvent } from 'model/event.model';
import { EventService } from 'services/event.service';
import { Component, OnInit } from '@angular/core';
import {GridOptions} from "ag-grid/main";

@Component({
  selector: 'event-details',
  templateUrl: './event-details.component.html',
  styleUrls: ['./event-details.component.css']
 })
export class EventDetailsComponent implements OnInit {
events: IEvent[];
gridOptions: any = [];
errorMessage: string;
error: any;

constructor(private eventService: EventService) {
  this.gridOptions = <GridOptions>{};
}

columnDefs() {
return [
    {headerName: "Event ID", field: "id", width: 100},
    {headerName: "Event Name", field: "name", width: 100},
    {headerName: "Event Date", field: "date", width: 100},
    {headerName: "Event Time", field: "time", width: 100},
    {headerName: "Event Price", field: "price", width: 100}
  ]
}

ngOnInit() {
  this.eventService.getEvents().subscribe(
    events =>  {
      this.events = events, 

    this.gridOptions = {
        rowData: this.events,
        columnDefs: this.columnDefs()
      };
    },
    error => this.errorMessage = <any>error);
}

onGridReady(params) {
    params.api.sizeColumnsToFit();
}

      

}

Here is the code for event.service.ts:

import { Injectable } from "@angular/core";
import  {Observable} from "rxjs/Rx";
import { Http } from "@angular/http";
import { IEvent } from 'model/event.model';
import 'rxjs/Rx';

@Injectable()
export class EventService {

  constructor(private http: Http) {}

  getEvents(): Observable<IEvent[]> {
    return this.http.get("/api/events")
        .map((response: any) => {
            return response.json();
        })
        .do(data => console.log(JSON.stringify(data)))
        .catch(this.handleError);
   };

  private handleError (error: any) {
    let errMsg = (error.message) ? error.message :
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }

      

}

+3


source to share





All Articles