【问题标题】:@angular/forms FormBuilder pops Property does not exist on type@angular/forms FormBuilder pops 类型不存在属性
【发布时间】:2023-03-09 12:34:01
【问题描述】:

对于我尝试引用输入到表单中的值的每个实例,我都会收到这个有趣的Property 'notes' does not exist on type '{ [key: string]: AbstractControl; }'. 错误。造成这种情况的行是'notes': this.addForm.controls.notes.value。我做错了什么?

这是错误的整个上下文

import { FormGroup, FormBuilder, Validators } from '@angular/forms';

export class TheNewClass {
    addApi(): void {
        if (this.addApiForm.valid) {
            Api.insert({
                'notes': this.addApi.controls.notes.value
            });
        }
    }
}

这是从中检索值和调用方法的形式。

<form [formGroup]="addApiForm" (ngSubmit)="addApi()" class="inline-form">
    <div class="form-group">
        <label for="apiNotes">Notes</label>
        <input id="apiNotes" formControlName="apiNotes" class="form-control" type="text" placeholder="Notes">
    </div>
    <button type="submit" class="btn btn-primary">Add</button>
</form>

【问题讨论】:

  • 你能再分享一些代码吗?
  • @ranakrunal9 确定
  • 您如何定义您在TheNewClass 中访问的addApi formGroup?

标签: angular angular2-forms angular2-formbuilder


【解决方案1】:

FormControl 可以这样访问:

'notes': this.addApiForm.controls['notes'].value

【讨论】:

  • 就是这样……太简单了我会在时间限制到时标记为答案。
猜你喜欢
  • 2023-03-18
  • 2017-05-06
  • 2018-07-12
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-23
  • 2021-04-02
  • 2021-12-26
相关资源
最近更新 更多