我有
var body = {username:"ali", password:"p"};
this.http.post('http://localhost:27017/user/auth', body).subscribe(data =>
{console.log(data);});
如何从表单加载带有一些数据绑定(bind)的变量主体?我只是进行登录 - 我有两个字段,用户输入的用户名和密码,然后将进入第二个 .post() 参数。
最佳答案
html代码
<input type="text" id="username" required [(ngModel)]="user.username">
<input type="password" id="password" required [(ngModel)]="user.password">
<button (click)="submit()">submit</button>
ts代码
user={username:"", password:""};
submit(){
const body = this.user;
this.http.post('http://localhost:27017/user/auth', body).subscribe(data =>
{console.log(data);});
}
编辑:您当然需要为表单添加测试以获取更多信息https://angular.io/guide/forms
关于javascript - Angular 将表单中的代码放入 [POST],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46353399/