Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component({
- template: `
- <form [formGroup]="formGroup" novalidate (ngSubmit)="onSubmit()">
- <input type="file" (change)="onFileChange($event)" />
- <button type="submit" [disabled]="formGroup.invalid || formGroup.prestine">Submit</button>
- </form>
- `
- })
- export class PizzaComponent {
- formGroup = this.fb.group({
- file: [null, Validators.required]
- });
- constructor(private fb: FormBuilder, private cd: ChangeDetectorRef) {}
- onFileChange(event) {
- const reader = new FileReader();
- if(event.target.files && event.target.files.length) {
- const [file] = event.target.files;
- reader.readAsDataURL(file);
- reader.onload = () => {
- this.formGroup.patchValue({
- file: reader.result
- });
- // need to run CD since file load runs outside of zone
- this.cd.markForCheck();
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement