角材料反应形式无法正确获取形式值

时间:2018-08-31 14:06:30

标签: angular angular-material image-uploading angular-reactive-forms color-picker

我有一个反应式表格,我想上传一张图像和两个彩色十六进制值,然后将数据发送给数据库。我正在使用Angular Material和颜色选择器插件,但是当我选择图像或颜色并尝试提交表单时,唯一记录的信息是userId(来自DataService),其他表单字段为空。相反,如果我手动编写某些内容,那么我的字段会带有小值。这是.ts文件:

customize.component.ts

  import { Component, OnInit, ViewContainerRef } from '@angular/core';
  import { HttpClient, HttpEventType } from '@angular/common/http';
  import { slideInOutAnimation } from '../animations';
  import { CustomizeApiService } from './customize-api.service';
  import { FormGroup, FormControl, FormGroupDirective, NgForm, FormBuilder, Validators, AbstractControl } from '@angular/forms';
  import { ErrorStateMatcher } from '@angular/material';
  import { Router } from '@angular/router';
  import { DataService } from '../services/data.service';
  import { ColorPickerService, Cmyk } from 'ngx-color-picker';

  /** Error when invalid control is dirty, touched, or submitted. */
  /** TODO copy error matcher in all components */
  export class MyErrorStateMatcher implements ErrorStateMatcher {
    isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
      const isSubmitted = form && form.submitted;
      return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
    }
  }

  @Component({
    selector: 'app-customize',
    templateUrl: './customize.component.html',
    styleUrls: ['./customize.component.css'],
    // make slide in/out animation available to this component
    animations: [slideInOutAnimation],
    // attach the slide in/out animation to the host (root) element of this component
    // tslint:disable-next-line:use-host-property-decorator
    host: { '[@slideInOutAnimation]': '' }
  })
  export class CustomizeComponent implements OnInit {

    userId = '';
    selectedFile: File = null;
    imageFileName = '';
    url = '../../assets/images/placeholder.png';
    customizeForm: FormGroup;
    matcher = new MyErrorStateMatcher();

    SCZ_SU_ID: '';
    SCZ_LOGO_URL: '';
    SCZ_MAIN_COLOR: '';
    SCZ_SECONDARY_COLOR: '';

    public mainColor = '#ffffff';
    public secondaryColor = '#ffffff';

    constructor(
      private data: DataService,
      private router: Router,
      private http: HttpClient,
      private api: CustomizeApiService,
      private formBuilder: FormBuilder,
      public vcRef: ViewContainerRef,
      private cpService: ColorPickerService
    ) { }

    ngOnInit() {
      this.data.getUserFromToken().subscribe(utente => {
        this.userId = utente['SU_ID'];
        console.log(this.userId);
        this.setFormValues(utente['SU_ID']);
      });
      this.customizeForm = this.formBuilder.group({
        'SCZ_SU_ID': [null],
        'SCZ_LOGO_URL': [null],
        'SCZ_MAIN_COLOR': [null],
        'SCZ_SECONDARY_COLOR': [null]
      });
    }

    onFileSelected(event) {
      if (event.target.files && event.target.files[0]) {
        this.selectedFile = event.target.files[0];
        this.imageFileName = this.selectedFile.name;
        // console.log(this.selectedFile.name);
        const reader = new FileReader();
        reader.readAsDataURL(event.target.files[0]); // read file as data url
        reader.onload = (_event) => { // called once readAsDataURL is completed
          this.url = _event.target.result;
        };
      }
    }

    setFormValues(userId) {
      this.customizeForm.setValue({
        SCZ_SU_ID: userId,
        SCZ_LOGO_URL: '',
        SCZ_MAIN_COLOR: '',
        SCZ_SECONDARY_COLOR: ''
      });
    }

    onMainColorChange(mainColor: any): void {
      this.mainColor = mainColor;
    }

    onSecondaryColorChange(secondaryColor: any): void {
      this.secondaryColor = secondaryColor;
    }

    onFormSubmit(form: NgForm) {
      console.log(form);
      // this.api.postCustomization(form)
      //   .subscribe(res => {
      //     alert('file uploaded');
      //     this.router.navigate(['/sks']);
      //   }, (err) => {
      //     console.log(err);
      //   });
    }
  }

这是html组件文件:

customize.component.html

 <div class="container">

    <mat-card>
        <form [formGroup]="customizeForm" (ngSubmit)="onFormSubmit(customizeForm.value)">
            <img mat-card-image height="200" width="200" [src]="url" alt="Photo Preview">
            <div class="button-row">
                <mat-form-field class="imageName">
                    <input matInput placeholder="Nome Immagine" formControlName="SCZ_LOGO_URL" [errorStateMatcher]="matcher" [value]="imageFileName"
                        (click)="imgFileInput.click()">
                    <mat-error>
                        <span *ngIf="!customizeForm.get('SCZ_LOGO_URL').valid && customizeForm.get('SCZ_LOGO_URL').touched">Scegli
                            un'immagine per il tuo logo</span>
                    </mat-error>
                </mat-form-field>
                <input hidden type="file" #imgFileInput (change)="onFileSelected($event)" />
            </div>
            <mat-form-field class="example-full-width">
                <input matInput placeholder="Colore principale" [value]="mainColor" formControlName="SCZ_MAIN_COLOR" [errorStateMatcher]="matcher"
                    [style.background]="mainColor" [colorPicker]="mainColor" [cpPosition]="'right'" (colorPickerChange)="onMainColorChange($event)">
                <mat-error>
                    <span *ngIf="!customizeForm.get('SCZ_MAIN_COLOR').valid && customizeForm.get('SCZ_MAIN_COLOR').touched">Scegli
                        il colore principale
                    </span>
                </mat-error>
            </mat-form-field>

            <mat-form-field class="example-full-width">
                <input matInput placeholder="Colore secondario" formControlName="SCZ_SECONDARY_COLOR" [errorStateMatcher]="matcher" [style.background]="secondaryColor"
                    [colorPicker]="secondaryColor" [cpPosition]="'right'" [value]="secondaryColor" (colorPickerChange)="onSecondaryColorChange($event)">
                <mat-error>
                    <span *ngIf="!customizeForm.get('SCZ_SECONDARY_COLOR').valid && customizeForm.get('SCZ_SECONDARY_COLOR').touched">Scegli
                        il colore secondario</span>
                </mat-error>
            </mat-form-field>

            <div class="button-row">
                <a mat-stroked-button color="basic" [routerLink]="['/sks']" class="mat-stroked-button">
                    <mat-icon>keyboard_backspace</mat-icon>
                </a>
                <button type="submit" [disabled]="!customizeForm.valid" mat-raised-button color="primary">
                    <mat-icon>cloud_upload</mat-icon>
                </button>
            </div>

        </form>
    </mat-card>
</div>

对于这里的完整项目,我提供了StackBlitz:https://stackblitz.com/github/ufollettu/SEANSA

有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

只需尝试删除[value]="mainColor"[value]="secondaryColor"

然后将onMainColorChange和其他功能更改为此类代码

  onMainColorChange(mainColor: any): void {
    this.customizeForm.patchValue({ SCZ_MAIN_COLOR: mainColor })
  }