Advertisement
filhotecmail

Providers

May 14th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Http, Response } from '@angular/http';
  3. import { Observable } from 'rxjs/Observable';
  4. import 'rxjs/add/operator/catch';
  5. import 'rxjs/add/operator/map';
  6.  
  7. /*
  8.   Generated class for the RestApiProvider provider.
  9.  
  10.   See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  11.   for more info on providers and Angular DI.
  12. */
  13. @Injectable()
  14. export class RestApiProvider {
  15.  
  16.   private apiUrl = 'http://127.0.0.1:8080/Datasnap/Rest/TServerMethods1/RequestOrderProduct';
  17.   public Dados: any
  18.   constructor(public http: Http) {
  19.     console.log('Hello RestApiProvider Provider');
  20.     this.ResultTeste;      
  21.   }
  22.  
  23.  
  24.   getprodutos(): Observable<string[]> {
  25.     return this.http.get(this.apiUrl)
  26.                     .map(this.extractData)
  27.                     .catch(this.handleError);                              
  28.   }
  29.  
  30.  
  31.  
  32.   private extractData(res: Response) {
  33.     let body = res.json();
  34.     return body || { };
  35.   }
  36.  
  37.   ResultTeste() {
  38.     this.Dados = this.getprodutos();
  39.     console.log('Teste '+ this.Dados);
  40.   }
  41.  
  42.   private handleError (error: Response | any) {
  43.     let errMsg: string;
  44.     if (error instanceof Response) {
  45.       const body = error.json() || '';
  46.       const err = body.error || JSON.stringify(body);
  47.       errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  48.     } else {
  49.       errMsg = error.message ? error.message : error.toString();
  50.     }
  51.     console.error(errMsg);
  52.     return Observable.throw(errMsg);
  53.   }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement