我升级到 rc.4 并尝试使用新的 forms-api。这是我得到的:
“类型错误:this.form.updateValueAndValidity 不是函数”
异常起源于 Angular 内的“form_group_directive.js”文件:
FormGroupDirective.prototype.ngOnChanges = function (changes) {
this._checkFormPresent();
if (collection_1.StringMapWrapper.contains(changes, 'form')) {
var sync = shared_1.composeValidators(this._validators);
this.form.validator = validators_1.Validators.compose([this.form.validator, sync]);
var async = shared_1.composeAsyncValidators(this._asyncValidators);
console.log('from within angular:---------------------------------------------------------------------------------------------------');
console.log(this.form);
this.form.asyncValidator = validators_1.Validators.composeAsync([this.form.asyncValidator, async]);
this.form.updateValueAndValidity({ onlySelf: true, emitEvent: false });
}
console.log 语句的输出是一个 FormGroupDirective
,它只是没有名为 updateValueAndValidity
的方法。
上述错误消息对任何人都意味着什么吗?
最佳答案
我认为您不能同时使用模板驱动方法和模型驱动方法。
所以我会使用以下任一方法:
<form *ngIf="showForm" #userForm="ngForm"
(ngSubmit)="userFormSubmit()">
</form>
或:
<form *ngIf="showForm" [formGroup]="userForm"
(ngSubmit)="userFormSubmit()">
</form>
其中 userForm
是在您的组件类中使用 FormBuilder
或 FormControl
定义的。我认为这种方法正是您所需要的...
关于angular 2 : TypeError: this. form.updateValueAndValidity 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38199046/