代码之家  ›  专栏  ›  技术社区  ›  Rupesh

如何在角度4中为窗体组中的键添加值[复制]

  •  0
  • Rupesh  · 技术社区  · 6 年前

    我想提交一份包含文件的表格。 所以我用 ng2-file-upload

    component.html 是-

    <input type="file" ng2FileSelect [uploader]="uploader" (onFileSelected)="onFileSelected($event)">
    

    我的组件是-

    public uploader: FileUploader = new FileUploader({});
    
      uploadForm: FormGroup;
    
      constructor(
        private formBuilder: FormBuilder,
        private userService: UserServiceService
      ) {
      }
    
      ngOnInit() {
    
        this.uploadForm = this.formBuilder.group({
          branch         : ['cs', Validators.required],
          document       : [null, Validators.required],
          semester       : ['3', Validators.required],
        });
    
    
      }
    
    
      //////////// File Converted into base64/////////////////
    
    
      onFileSelected(event: EventEmitter<File[]>) {
        const file: File = event[0];
    
        readBase64(file)
          .then(function(data) {
             console.log(data);
    
            this.uploadForm.patchValue({  // Showing error -- Potentially invalid reference access to a class field via 'this.' 
              document: data
            });
          });
    
      }
    

    我正在生成文件的base64 onFileSelected() document 输入我的 formGroup this.uploadForm.patchValue()

    Potentially invalid reference access to a class field via 'this.'
    

    我只是想补充一下 data 在我的 钥匙 uploadForm

    1 回复  |  直到 6 年前
        1
  •  0
  •   Antoniossss    6 年前

    或者

      .then(function(data) {
         this.whateva
        }.bind(this)); //here is the trick
    

      .then(data=> {//here is the trick
         this.whateva
        }); 
    

    我没有读剩下的代码。