src/app/app-poll/app-poll.service.ts
Properties |
|
Methods |
|
constructor(http: HttpClient)
|
||||||
Defined in src/app/app-poll/app-poll.service.ts:10
|
||||||
Parameters :
|
getPolls |
getPolls()
|
Defined in src/app/app-poll/app-poll.service.ts:15
|
Returns :
Observable<AppPoll[]>
|
Private handleError | ||||
handleError(err)
|
||||
Defined in src/app/app-poll/app-poll.service.ts:26
|
||||
Parameters :
Returns :
any
|
Private pollUrl |
pollUrl:
|
Type : string
|
Default value : 'api/polls'
|
Defined in src/app/app-poll/app-poll.service.ts:10
|
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {AppPoll} from './app-poll';
import { Observable, of, throwError } from 'rxjs';
import { catchError, tap, map } from 'rxjs/operators';
@Injectable({providedIn: 'root'})
export class AppPollService{
private pollUrl ='api/polls';
//pollsData : AppPoll[];
constructor(private http: HttpClient){}
getPolls(): Observable<AppPoll[]>{
return this.http.get<AppPoll[]>(this.pollUrl)
.pipe(
tap(data=> {
//this.pollsData = data;
// console.log(JSON.stringify(data))
}),
catchError(this.handleError)
)
}
private handleError(err) {
// in a real world app, we may send the server to some remote logging infrastructure
// instead of just logging it to the console
let errorMessage: string;
if (err.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
errorMessage = `An error occurred: ${err.error.message}`;
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
errorMessage = `Backend returned code ${err.status}: ${err.body.error}`;
}
console.error(err);
return throwError(errorMessage);
}
}