简体   繁体   English

动态添加 <input> 没有被渲染

[英]Dynamically added <input> not being rendered

I have a string which holds a form that I want to dynamically render. 我有一个字符串,其中包含我想要动态呈现的表单。

When the user clicks "Add Step", my template string is added to a list and rendered with an ngFor . 当用户单击“添加步骤”时,我的模板字符串将添加到列表中并使用ngFor渲染。 The problem is that the input doesn't render, only the <h3> tag. 问题是输入不呈现,只有<h3>标记。

Before button click: 点击按钮之前:

在此输入图像描述

After - no input is rendered on screen or in the html: 之后 - 屏幕上或html中没有呈现任何输入:

在此输入图像描述

ts: TS:

scenarioTemplate = `
  <div id="content">
    <h3>Name</h3>
    <input name="{{i}}" [ngModel]="i">
  </div>`;

steps = [];

addScenarioStep() {
    this.steps.push(this.scenarioTemplate);
}

html: HTML:

<form #myForm="ngForm">
  <fieldset ngModelGroup="inputs" #inputs="ngModelGroup">
    <ng-container *ngFor="let step of steps; let i = index">
      <div [innerHTML]=step attr.stepId="{{i}}"></div>
    </ng-container>
  </fieldset>
</form>
<button id="addStepBtn" type="button" class="btn btn-light" (click)="addScenarioStep()">Add Scenario Step +</button>
  1. Put that whole thing 把那整件事
  <div id="content">
    <h3>Name</h3>
    <input name="{{i}}" [ngModel]="i">
  </div>

in the template, not the TS. 在模板中,而不是TS。

  1. Don't mutate the array. 不要改变数组。 Instead of 代替
this.steps.push(newElem);

do

this.steps = [...this.steps, newElem];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM