【问题标题】:Error: Cannot find control with path: 'x ' angular 2错误:找不到带有路径的控件:'x' angular 2
【发布时间】:2017-12-09 15:36:56
【问题描述】:

我正在尝试使整个表单中的特定表单字段动态化,以便可以将 x 数量的对象添加到该字段的数组中。 但是,每次页面启动时我都会得到一个

错误:找不到带有路径的控件:'media ->'

这是我的表单.ts

this.cardForm = new FormGroup({
  'title': new FormControl(cardTitle),
  media: this._fb.array([
    this.initMedia(),
  ]),
  'links': new FormGroup({
    'news': new FormControl(news),
  }),

  initMedia() {
   return this._fb.group({
   type: new FormControl(),
   raw: new FormControl(),
   primary: new FormControl(),
   thumbs: this._fb.group({
    default: new FormControl()
  })
})
}

addMedia(){
 const control = <FormArray>this.cardForm.controls['media'];
 control.push(this._fb.control(['']));
}
removeMedia(i: number){
 const control = <FormArray>this.cardForm.controls['media'];
 control.removeAt(i);
}

这是我的 form.html:

<div class="row">
  <div class="col-xs-12">
    <form [formGroup]="cardForm" (ngSubmit)="onSubmit(cardForm.value)">
      <div class="row">
        <div class="col-xs-12">
          <button
            type="submit"
            class="btn btn-success">
            Update Card</button>
          <button
            type="button"
            class="btn btn-danger"
            (click)="onCancel()">
            Cancel</button>
        </div>
      </div>

    <div formArrayName="media">
      <div class="row">
        <div class="col-xs-12">
          <div class="form-group">
            <div *ngFor= "let media of cardForm.controls.media.controls; let i=index">
              <span>Media {{i + 1}}</span>
              <span *ngIf="cardForm.controls.media.controls.length > 1" (click)="removeMedia(i)"></span>
          </div>
          <div [formGroupName]="i">
            <div>
              <label>Url</label>
                <md-input-container class="mdcontainer">
                  <input mdInput placeholder="Media Url" type="text" formControlName="raw">
                </md-input-container>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

而 media[] 看起来像这样:

media: [
        {
          raw:'string',
          primary: boolean,
          type: 'string',
          thumb: {
                 default: 'string'
                 {
        }
      ]

我在这里遗漏了什么/做错了什么导致该错误出现? 任何帮助/提示/建议将不胜感激。

【问题讨论】:

  • 尝试运行 ng build --prod --aot 并收到错误“ng 中的错误:文件路径:“AbstractControl”类型上不存在属性“控件” . 文件路径和脚本行指向

标签: html angular angular2-forms


【解决方案1】:

您可以尝试使用 get 方法。我觉得它更人性化一点:

cardForm.get('media')

在此处的文档中查看:https://angular.io/guide/reactive-forms#inspect-formcontrol-properties

【讨论】:

    【解决方案2】:

    [formGroupName]="i" 应该在*ngFor 的内部。在这种情况下,i 变量将具有非 undefined

    <div *ngFor="let media of cardForm.controls.media.controls; let i=index">
      <span>Media {{i + 1}}</span>
      <span *ngIf="cardForm.controls.media.controls.length > 1" (click)="removeMedia(i)"></span>
      <div [formGroupName]="i">
        <div>
          <label>Url</label>
          <md-input-container class="mdcontainer">
            <input mdInput placeholder="Media Url" type="text" formControlName="raw">
          </md-input-container>
        </div>
      </div>
    </div>
    

    Plunker Example

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 2021-09-09
      • 2017-10-28
      • 2020-08-18
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多