英文:
how to send a form data to server using post api in angular 7
问题
如何使用Angular发送表单数据使用POST API,已经在组件.ts文件中使用了表单和方法,
<form #hlForm="ngForm" (ngSubmit)="getHospitalList(hlForm)">
<input
type="text"
class="input-texts"
name="name"
placeholder="输入医院、保险或地点"
ngModel
required
>
<button
type="submit"
value="submit"
class="hl-btn"
(click)="hospitalFirstFold = false; hospitalSecondFold = true;"
>搜索
</button>
</form>
getHospitalList(form){
console.log(form.value)
}
英文:
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)
}
答案1
得分: 5
你需要创建表单数据以使用POST API发送数据。
let fd = new FormData();
fd.append(key, value);
对于API调用:
this.http.post(url, fd);
根据需要填写FormData中的值。
英文:
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
答案2
得分: 3
- 创建一个服务文件。
- 在你的类中创建一个自己选择的函数,例如,名为 function1 的函数。
function1(formData) {
return this.http.post(url, formData);
}
在构造函数中添加 http 并导入它。
constructor (private http: HttpClient) {}
- 在你的类中以表单作为参数调用函数 function1,并订阅它。
getHospitalList() {
this.serviceA.function1(form.value).subscribe(response) =>
{console.log(response)});
}
英文:
- Create a service file.
- 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) {}
- Call that function1 in your class with form as a parameter and subscribe
getHospitalList() {
this.serviceA.function1(form.value).subscribe(response) =>
{console.log(response)});
}
</details>
# 答案3
**得分**: 2
开始通过导入必要的API:
```typescript
import { HttpClient } from '@angular/common/http';
声明如下的提交函数:
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)
);
}
英文:
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)
);
}
答案4
得分: 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 form:
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?
英文:
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??
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论