输入类型=“文件”的角度2上没有形式验证

时间:2017-03-02 03:57:09

标签: forms angular

  <div class="form-group">
    <label for="fileName">Name</label>

    <input type="file" class="form-control" id="fileName"  placeholder="Upload file" accept=".xlsx, .xls"
           required
           [(ngModel)]="model.fileName" name="fileName"
           #fileName="ngModel">
    <div [hidden]="fileName.valid || fileName.pristine" class="alert alert-danger">
      File to be uploaded is required
    </div>
  </div>

使用angularjs2表单,我正在尝试上传文件并提交表单。即使我选择了文件,控件也无法识别所选值。

1 个答案:

答案 0 :(得分:6)

正如其他人所说,Angular不支持输入类型=文件(但?)。你有一些解决方法

//template
<input type="file" class="form-control" id="fileName"  placeholder="Upload file" accept=".xlsx, .xls"
           required
           (change)="onImageChangeFromFile($event)">

//component
onImageChangeFromFile(event) {
    if (event.target.files && event.target.files[0]) {
      let file = event.target.files[0];
      //validation here then attribute the value to your model
      this.model = file
    }
  }