如何将表单数据从角度传递到nodejs

2023-12-27

我是 Angular5 的新手。我需要将用户详细信息从 Angular 传递到 Nodejs。

应用程序组件.ts:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl, FormArray } from 
'@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    constructor(private http:Http) { } 
    onSubmit(registerForm) {
       console.log(registerForm.value);
       let url = 'http://localhost:8080/signup';
       this.http.post(url, {registerForm(registerForm)}).subscribe(res => 
          console.log(res.json()));
    }
 }

现在我需要将这些数据传递给nodejs 路由以进一步进行。

Node.js 路由文件:

module.exports = function(app, passport) {

app.post('/signup', passport.authenticate('local-signup', {
    successRedirect : '/', 
    failureRedirect : '/',
    failureFlash : true 
}));

}; 

现在出现以下错误:未捕获错误:无法解析 AppComponent 的所有参数:(?)。


从以下位置调用您的函数component.html文件它将触发您的函数component.ts文件。 从此函数调用服务,其中包含将请求您的节点 API 的函数

addData() {
    this.adminService.addCountry(this.form.value).subscribe(
      res => {
        var response = res.json();
        this.flashMessagesService.show(response.message, {
          cssClass: "alert-success",
          timeout: 2000
        });
      },
      error => {
        if (error.status == 401) {
          localStorage.removeItem("currentUser");
          this.router.navigate(["/"]);
        } else {
          this.flashMessagesService.show(error.json().error, {
            cssClass: "alert-danger",
            timeout: 2000
          });
        }
      }
    );
  }

创建管理服务来调用在节点上运行的 HTTP URL

Service

  addCountry(formData) {
    console.log(formData);
    var authToken = this.getAuthToken();
    if (authToken != "") {
      var headers = this.getHeaders();
      headers.append("Authorization", authToken);
      return this.http
        .post(
          `http://localhost:3000/addData`,
          this.formData(formData),
          { headers: headers }
        )
        .map((response: Response) => {
          return response;
        });
    }
  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将表单数据从角度传递到nodejs 的相关文章

随机推荐