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

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

问题描述

在我的 Angular 2 表单中,我有以下打字稿:

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);
    }

    ....
}

和模板html:

<form class="form-horizontal" #userEditForm="ngForm" (ngSubmit)="onSaveUser()" novalidate>
    ....
</form>

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

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.

首先,resetForm 是一个实际有效的函数吗?我似乎找不到它的任何文档......但它有效而 .reset() 没有.

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.

第二,我怎样才能摆脱这个 TypeScript 错误?我需要更新 Angular 2 的类型吗?

Second, how can I get rid of this TypeScript error? Do I need to update my typings for Angular 2?

推荐答案

Declare userEditForm as NgForm像这样:

Declare userEditForm as NgFormlike this:

@ViewChild("userEditForm") userEditForm: NgForm;

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

Now you can access .resetForm() method.

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

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