How to consume an API using Angular 6?

Asked

Viewed 664 times

0

How to get feedback from the post 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>
  • If you have no problem reading in English you have a great explanation here about http responses using https://scotch.io/tutorials/angular-2-http-requests-with-observables

  • @Herick what is the return of the post?

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.