将表单中的角度放入[POST]

时间:2017-09-21 21:24:52

标签: javascript angular typescript http data-binding

我有

var body = {username:"ali", password:"p"};
    this.http.post('http://localhost:27017/user/auth', body).subscribe(data => 
{console.log(data);});

如何使用表单中的某些数据绑定来加载变量主体?我只是登录 - 我有两个字段,用户输入的用户名和密码,然后它将进入第二个.post()参数。

1 个答案:

答案 0 :(得分:1)

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