Home > Back-end >  sending data to api with all data from form using angular
sending data to api with all data from form using angular

Time:04-28

I am new so don't mistake me if i asked small questions or wrong questions

<form >
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lname"><br><br>
</form>
<button>submit<button>

I want to send this all value to api through angular

CodePudding user response:

If you're using angular you should know about : https://material.angular.io/guides

your angular html

<form [formgroup] = formname>
  <label for="fname">First name:</label>
  <input formControlname=name type="text" id="fname" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input formControlname=name type="text" id="lname" name="lname"><br><br>
</form>
<button (click)="submitdata()">submit<button>

your TS file

import{HttpClient}............
import{FormBuilder, FormGroup, Validators, FormControl}........
const(private formbuilder:FormBuilder, private http:HttpClient)
formname!:formgroup;
ngOnInit{
this.formname = this.formbuilder.group({
name : ['',validators.required];
lname : ['', validators.required];
})
}
submitdata(){
this.http.post('your_url', this.formname.value)
}


  • Related