Difference between revisions of "Using Angular HttpClient"

From PeformIQ Upgrade
Jump to navigation Jump to search
Line 26: Line 26:
</pre>
</pre>


Get a list of markers - this.markers:


<pre>


  getMarkers() {
    const url = this.baseUrl + '/v1/xxx/xxxx/markers';
    // console.log('[getMarkers]                                  url |' + url + '|');
    this.httpClient.get(url).subscribe((markers: Marker[]) => {
        // console.log('[getMarkers]                        markers |');
        // console.log(markers);
        this.markers = markers;
    },
    error => { this.errorMessage = error as any; },
    () => {
      // console.log('[getMarkers]            COMPLETED => markers |' + this.markers + '|');
      // Complete
    });
  } // getMarkers
</pre>


==POST==
==POST==

Revision as of 10:47, 24 October 2019

Links

Examples

Some examples.

GET

  getUsers() {
    this.httpClient.get(this.url').subscribe((res: any[]) => {
        console.log(res);
        this.users = res;
    },
    error => { this.errorMessage = error as any; },
    () => {
      // Complete - update a datatable
      this.changeDetectorRef.detectChanges();
      const table: any = $('#UsersTable');
      this.dataTable = table.DataTable(this.dtOptions);
    });

  } // getUsers

Get a list of markers - this.markers:


  getMarkers() {

    const url = this.baseUrl + '/v1/xxx/xxxx/markers';

    // console.log('[getMarkers]                                  url |' + url + '|');

    this.httpClient.get(url).subscribe((markers: Marker[]) => {

        // console.log('[getMarkers]                        markers |');
        // console.log(markers);

        this.markers = markers;
    },
    error => { this.errorMessage = error as any; },
    () => {
      // console.log('[getMarkers]            COMPLETED => markers |' + this.markers + '|');
      // Complete
    });

  } // getMarkers

POST


PATCH

  update(xxx: Xxx) {

    this.httpClient.patch(this.baseUrl + '/v1/xxx/xxxx-xxxx/' + this.id,
    {
      Id : xxx.Id,
      Status: xxx.Status,
      XxxId: xxx.XxxId,
      NoXxx: xxx.NoXxx
    })
    .subscribe(
      data  => {
        console.log('[update]              PATCH Request is successful |' + data + '|');
        return data;
      },
      error  => {
        console.log('[update]                                    Error |' + error + '|');
        return  error;
      });

  } // update