2

how can I send form data using post API using angular , have used form and method for component.ts file ,

<form #hlForm="ngForm" (ngSubmit)="getHospitalList(hlForm)">
  <input 
   type="text" 
   class="input-texts" 
   name="name" 
   placeholder="Enter Hospital , Insurance or Location"
   ngModel
   required
  >
  <button 
  type="submit" 
  value="submit" 
  class="hl-btn"
  (click)="hospitalFirstFold = false; hospitalSecondFold = true;"
   >Search
   </button>
</form>

  getHospitalList(form){
    console.log(form.value)
  }

4 Answers 4

7

you need to create form data to send data using post api.

let fd = new FormData();
fd.append(key, value);

For api call

this.http.post(url, fd);

Fill values accordingly in formdata

Sign up to request clarification or add additional context in comments.

2 Comments

Are there any constraints on what value should be? Must it be form.value or can it be anything like string or number?
Value refers to what is required in the api. For ex: fd.append('age', '24').. Now this 24 might be coming from your some formControl or ngModel. I hope you can relate it now?
3

Start by importing the necessary APIs:

import { HttpClient } from '@angular/common/http';

Declare submit function like below:

getHospitalList(form){
    const formData = new FormData();
    formData.append('key', form.value);

    this.httpClient.post<any>(this.SERVER_URL, formData).subscribe(
      (res) => console.log(res),
      (err) => console.log(err)
    );
  }

Comments

3
  1. Create a service file.
  2. Create one function named of your choice, for example, function1 in your class.
function1(formData) {
return this.http.post(url, formData);
}

Add http in the constructor and import it.

constructor (private http: HttpClient) {}
  1. Call that function1 in your class with form as a parameter and subscribe
   getHospitalList() {
    this.serviceA.function1(form.value).subscribe(response) => 
      {console.log(response)});
    }

Comments

1
import HttpClientModule and formsmodule in your app module if you have not defined a different module.

import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';

*in your component*
import {NgForm} from '@angular/forms';

add serverurl as :
private readonly baseUrl = 'YourUrl';

constructor(private http: HttpClient) { }

create a method you gonna use for your submit event from your for ""

  getHospitalList(form: NgForm) {
    this.http.post(this.baseUrl, form.value).subscribe(next => {
      console.log("successful");
    },
    error => {
      console.log("failed submitting");
    });
  }

(click)="hospitalFirstFold = false; hospitalSecondFold = true;" what is going on there??

2 Comments

(click)="hospitalFirstFold = false; hospitalSecondFold = true;" what is going on there??
this for pagination, for sections, sorry for the confusion, it is unrelated to this part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.