How to consume an API using Angular 6?

0

How to get the postback through the Component?

Code:

service

import {Injectable} from '@angular/core'
import {HttpClient,HttpHeaders} from '@angular/common/http'
import {Observable} from 'rxjs/Observable'
import 'rxjs/add/operator/map'


import { error } from 'selenium-webdriver';

@Injectable()
export class AppService{
    constructor(private http:HttpClient){}

    sendEmail(email){
         return this.http.post('http://localhost:8080/backendteste/db/email.php?acao=sendEmail',email)
        .subscribe((data)=>{
            console.log('pegando retorno do backend ',data);    
        },(error)=>{
            console.log('ERROR: ',error);
        });
    }
    clear(){
        return true;
    }
}

component

export class AppComponent implements OnInit {
        statusEmail:string;
        constructor(private fb: FormBuilder, private appService: AppService) { }

        enviarEmail(form){
            this.appService.sendEmail(form);
           // console.log(form);
       }
    }

html

<div class="" *ngIf="statusEmail">
            <i class="rounded-x fa fa-check"></i>
            <p>Sua mensagem foi enviada com sucesso !</p>
          </div>
    
asked by anonymous 10.01.2018 / 15:42

1 answer

0

You only have to subscribe:

enviarEmail(form){
  this.appService.sendEmail(form).subscribe(response => {
    // retorno do http.post
  });
}

Read more about http requirements with Angular at: link

    
01.06.2018 / 18:27