Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 存储数据的表格_Angular_Angular Reactive Forms_Angular Forms - Fatal编程技术网

Angular 存储数据的表格

Angular 存储数据的表格,angular,angular-reactive-forms,angular-forms,Angular,Angular Reactive Forms,Angular Forms,我以前使用过模板驱动的表单,我对反应式表单如何将数据存储到数据库中有点困惑。最初我只使用[(ngModel)]=“user.date”。如何在提交时存储数据?我已经建立了一个如下: this.formGroup = this._formBuilder.group({ formArray: this._formBuilder.array([ this._formBuilder.group({ dateFormCtrl: ['', Validators.required]

我以前使用过模板驱动的表单,我对反应式表单如何将数据存储到数据库中有点困惑。最初我只使用[(ngModel)]=“user.date”。如何在提交时存储数据?我已经建立了一个如下:

this.formGroup = this._formBuilder.group({
  formArray: this._formBuilder.array([
    this._formBuilder.group({
      dateFormCtrl: ['', Validators.required]
    }),
    this._formBuilder.group({
      emailFormCtrl: ['', Validators.email]
    }),
  ])
});
下面是我要存储到db的输入示例:

<input formControlName="dateFormCtrl" matInput [matDatepicker]="picker" [max]="maxDate" [min]="minDate" placeholder="When is your event?"(click)="picker.open()" readonly>

你的表格应该是

 this.form = this._formBuilder.group({
       dateFormCtrl: ['', Validators.required],
       emailFormCtrl: ['', Validators.email]
    });
您的提交方法应该是

   onSubmit(): void{
       const formObj ={
        date: this.form.value.dateFormCtrl,
        email: this.form.value.emailFormCtrl,
       }
      this.http.post('http://localhost:3000/user', formObj).subscribe(res =>
    }

如果要将表单数据动态保存到服务器,则可以遵循以下代码。这是最好的选择

在component.ts文件中:

  form: FormGroup;
  payLoad = '';

    onSubmit() {
        this.payLoad = JSON.stringify(this.form.value);

        this.serverService.storeServers(this.payLoad)
        .subscribe(
          (response) => console.log(response),
          (error) => console.log(error)
        );

      }
在您的service.ts文件中:

constructor(private http: HttpClient) { }
  storeServers(payLoad: any) {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json'
      })
    };
    return  this.http.post(' ***Complete URL*** ', payLoad, httpOptions);
  }
constructor(private http: HttpClient) { }
  storeServers(payLoad: any) {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json'
      })
    };
    return  this.http.post(' ***Complete URL*** ', payLoad, httpOptions);
  }