Angular 2 FormControl.reset()不起作用,但.resetForm()会(但打字稿错误) [英] Angular 2 FormControl.reset() doesn't work, but .resetForm() does (but typescript error)

查看:531
本文介绍了Angular 2 FormControl.reset()不起作用,但.resetForm()会(但打字稿错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  export class UserEditComponent implements OnChanges {
@ Input()userModel:IUserModel;
@Output()onSave:EventEmitter< IUserModel> = new EventEmitter< IUserModel>();

@ViewChild(userEditForm)userEditForm:FormControl;

private userNameIsValid = true;

构造函数(private httpService:HttpService){
}

ngOnChanges(改变:SimpleChanges){
this.userEditForm.resetForm();
console.log(this.userEditForm);
}

....
}

和模板html:

 < form class =form-horizo​​ntal#userEditForm =ngForm(ngSubmit)= onSaveUser()novalidate> 
....
< / form>

我尝试使用.reset()函数,但它什么也没做。 .resetForm实际上将表单的_submitted属性从true重新设置为false,但是,它给了我一个TypeScript错误属性resetForm在类型FormControl 中不存在。



首先,resetForm是一个实际的函数吗?我似乎无法找到任何文件......但它的工作原理和.reset()不会。

第二,我该如何摆脱这个TypeScript错误?我需要更新Angular 2的类型吗?

解决方案

声明 userEditForm as NgForm 像这样:
$ b $ pre $ @ViewChild(userEditForm)userEditForm:NgForm;

现在您可以访问 .resetForm()方法。

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 as NgFormlike this:

@ViewChild("userEditForm") userEditForm: NgForm;

Now you can access .resetForm() method.

这篇关于Angular 2 FormControl.reset()不起作用,但.resetForm()会(但打字稿错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆