首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Angular 4中为FormGroup中的关键字添加值

如何在Angular 4中为FormGroup中的关键字添加值
EN

Stack Overflow用户
提问于 2018-08-11 05:31:26
回答 1查看 156关注 0票数 0

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

component.html是-

代码语言:javascript
复制
<input type="file" ng2FileSelect [uploader]="uploader" (onFileSelected)="onFileSelected($event)">

我的component.ts是-

代码语言:javascript
复制
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
        });
      });

  }

我在onFileSelected()函数中生成文件的base64,然后我想将该base64添加到我的formGroup中的document密钥中。但是当我尝试使用this.uploadForm.patchValue()时,它显示了一个错误

代码语言:javascript
复制
Potentially invalid reference access to a class field via 'this.'

我只想将该data添加到我的document密钥uploadForm

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-11 05:34:57

任一

代码语言:javascript
复制
  .then(function(data) {
     this.whateva
    }.bind(this)); //here is the trick

代码语言:javascript
复制
  .then(data=> {//here is the trick
     this.whateva
    }); 

我没有读剩下的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51794270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档