【发布时间】:2016-12-25 05:37:13
【问题描述】:
我正在 Ionic 2 中构建一个数据驱动的表单,其中包含几个字段。当表单的值发生变化时,我正在尝试手动更新一个控件的值。但是,我收到此错误:TypeError: _this.myForm.controls.name.setValue is not a function。我尝试了使用和不使用 <FormControl> 类型转换,但错误仍然存在。
import { Component } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
@Component({
templateUrl: 'build/pages/lead-provider/add-bp/add-bp.html',
directives: [REACTIVE_FORM_DIRECTIVES]
})
export class AddBpPage {
private myForm: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.myForm= formBuilder.group({
'name': ['', [Validators.required]],
'email': ['', [Validators.required]],
'phone': ['', [Validators.required]]
});
this.myForm.valueChanges.subscribe((value) => {
(<FormControl>this.myForm.controls['name']).setValue('abc');
});
}
}
【问题讨论】:
-
(<FormControl>this.myForm.controls['name'])返回什么?FormControl的演员表不再需要 AFAIK。你用的是哪个 Angular2 版本? -
Ionic2当前使用的版本,rc4。演员表返回一个 FormControl 对象
标签: angular typescript ionic2 angular2-forms