【问题标题】:Adding input fields to a table using Form Array and *ngFor - Angular 2/5使用 Form Array 和 *ngFor - Angular 2/5 向表中添加输入字段
【发布时间】:2018-11-29 02:38:06
【问题描述】:

我是 Angular 5 的初学者,尝试将输入字段动态添加到带有表单数组的表中。 我在 tr 标签中使用了 *ngFor,示例代码如下:

HTML 文件:

          <table class="table">
              <thead>
                  <tr>
                      <th>F1</th>
                      <th>F2</th>
                  </tr>
              </thead>
              <form novalidate role="form" [formGroup]="caseinfo">
              <div formArrayName="caseRows">
              <tbody>
                  <tr *ngFor="let caserow of caseinfo.controls.caseRows.controls; let i=index" [formGroupName]="i" >

                      <td>
                          <div class="form-group">
                              <input type="text" class="form-control" formControlName="caselpn">
                              </div>
                          </div>
                      </td>
                      <td>
                          <div class="form-group">
                              <input type="text" class="form-control" formControlName="casesku">
                          </div>
                      </td>
                  </tr>
              </tbody>
            </div>
            </form>

          </table>

TS 文件:

  initFormCase()
  {
    this.caseinfo = this.fb.group({
    caseRows: this.fb.array([this.initCaseRows()]) // here
    });
  }

  initCaseRows() {
    return this.fb.group({
    caselpn:[null, Validators.compose([
      Validators.required,
    ])],
    casesku:[null, Validators.compose([
      Validators.required,
    ])]
    });
    }

这是我的输出的样子: https://i.imgur.com/ds4meaL.png

预期输出: 在 F1 列下输入字段 1。和 在 F2 列下输入字段 2。

注意: 我也尝试替换 tbody 内的 ngFor 。没有运气。 提前致谢! 抱歉,由于我的代表点低,无法发布图片。

【问题讨论】:

    标签: angular angular5 ngfor formarray formgroups


    【解决方案1】:

    您的 HTML 结构错误,尝试如下更新并检查

    <form novalidate role="form" [formGroup]="caseinfo">
        <div formArrayName="caseRows">
            <table class="table">
                <thead>
                    <tr>
                        <th>F1</th>
                        <th>F2</th>
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let caserow of caseinfo.controls.caseRows.controls; let i=index" [formGroupName]="i">
                        <td>
                            <div class="form-group">
                                <input type="text" class="form-control" formControlName="caselpn">
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input type="text" class="form-control" formControlName="casesku">
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
    

    【讨论】:

    • 我猜结构是我的问题。在我进行了您建议的修改后,它开始正常工作。非常感谢!
    猜你喜欢
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2018-06-25
    • 2018-08-29
    相关资源
    最近更新 更多