【问题标题】:setValue is not a functionsetValue 不是函数
【发布时间】: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');
         });
     }
}

【问题讨论】:

  • (&lt;FormControl&gt;this.myForm.controls['name']) 返回什么? FormControl 的演员表不再需要 AFAIK。你用的是哪个 Angular2 版本?
  • Ionic2当前使用的版本,rc4。演员表返回一个 FormControl 对象

标签: angular typescript ionic2 angular2-forms


【解决方案1】:

setValue 是在 2 周前添加的。因此 RC.4 还不能包含它。

改用updateValueAndValidity()

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2021-10-04
    • 1970-01-01
    • 2021-05-15
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2020-03-10
    相关资源
    最近更新 更多