【问题标题】:angular put code from a form into a [POST]将表单中的角度代码放入 [POST]
【发布时间】:2018-03-03 08:45:43
【问题描述】:
我有
var body = {username:"ali", password:"p"};
this.http.post('http://localhost:27017/user/auth', body).subscribe(data =>
{console.log(data);});
如何通过表单中的一些数据绑定加载变量主体?我只是登录 - 我有两个字段,用户输入的用户名和密码,然后将进入第二个 .post() 参数。
【问题讨论】:
标签:
javascript
angular
typescript
http
data-binding
【解决方案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