向API发送数据,所有数据都来自form using angular



我是新人,所以如果我问了一些小问题或错误的问题,请不要误解我

<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>

我想通过angular

把所有的值发送给api

如果你正在使用angular,你应该知道:https://material.angular.io/guides

你的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>

您的TS文件

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)
}

最新更新