javascript - Angular 将表单中的代码放入 [POST]

标签 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);});

如何从表单加载带有一些数据绑定(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/

相关文章:

angular - 再次关于 Angular2 中的全局数据

返回属性名称字符串文字值的 TypeScript 函数?

javascript - 从 jquery 中的另一个 javascript 调用函数

javascript - 为什么 console.log() 不连接字符串?

angular - Formbuilder 和 Angular 材

angular - 从 td 获取值,在 Angular 中输入

TypeScript 问题 - TypeError : Cannot read property 'watchRun' of undefined

typescript - 获取类的所有可用属性

javascript - 获取所有 Angular Controller

JavaScript 和 CSS 样式在我的 "Google Sites"页面上不起作用?