In my Angular 2 form, I have the following typescript:
export class UserEditComponent implements OnChanges {
@Input() userModel: IUserModel;
@Output() onSave: EventEmitter<IUserModel> = new EventEmitter<IUserModel>();
@ViewChild("userEditForm") userEditForm: FormControl;
private userNameIsValid = true;
constructor(private httpService: HttpService) {
}
ngOnChanges (changes: SimpleChanges) {
this.userEditForm.resetForm();
console.log(this.userEditForm);
}
....
}
and template html:
<form class="form-horizontal" #userEditForm="ngForm" (ngSubmit)="onSaveUser()" novalidate>
....
</form>
I tried using the .reset() function, but it did nothing. The .resetForm actually resets the _submitted property of the form from true back to false, however, it gives me a TypeScript error Property resetForm does not exist on type FormControl
.
First, is resetForm an actual function that works? I can't seem to find any documentation for it... but it works and .reset() doesn't.
Second, how can I get rid of this TypeScript error? Do I need to update my typings for Angular 2?
Declare
userEditForm
asNgForm
like this:Now you can access
.resetForm()
method.You should import NgForm from @angular/forms and try this.