Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.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
Javascript 如何在<;输入类型=";文件"&燃气轮机;角度4_Javascript_Angular_Validation - Fatal编程技术网

Javascript 如何在<;输入类型=";文件"&燃气轮机;角度4

Javascript 如何在<;输入类型=";文件"&燃气轮机;角度4,javascript,angular,validation,Javascript,Angular,Validation,文本输入工作正常。我使用相同的文件,但它不工作 <input type="text" name="title" #title="ngModel" ngModel required class="form-control" placeholder="Title Here"> <div *ngIf="title.errors && (title.dirty || title.touched)"> <span class="help-block e

文本输入工作正常。我使用相同的文件,但它不工作

<input type="text" name="title" #title="ngModel" ngModel required class="form-control" placeholder="Title Here">
<div *ngIf="title.errors && (title.dirty || title.touched)">
    <span class="help-block error" [hidden]="!title.errors.required"> Title 
      is Required 
    </span>
</div>
<input type="file" #image="ngModel" (change)="fileEvent($event)" 
name="image" ngModel>

标题
是必需的

如果只想显示/隐藏错误消息,则取决于所选文件

@Component({
  selector: 'my-app',
  template: `
    <div>
       <input type="file" (change)="onChange($event)" name="image">
      <div *ngIf="!fileSelected">
      <span class="help-block error" [hidden]="!fileSelected"> 
        File is Required 
      </span>
     </div>
    </div>
  `,
})
export class AppComponent {
  onChange(event) {
    var files = event.srcElement.files;
    console.log(files);
    this.fileSelected = files.length>0;
  }
}
@组件({
选择器:“我的应用程序”,
模板:`
文件是必需的
`,
})
导出类AppComponent{
onChange(事件){
var files=event.srcmelement.files;
console.log(文件);
this.fileSelected=files.length>0;
}
}
查看我的答案,了解完整的工作示例和说明