【问题标题】:Cloned elements cannot be submitted in Angular4克隆的元素不能在 Angular4 中提交
【发布时间】:2018-04-01 15:40:15
【问题描述】:

我有一个模板,其中包含两个字段,例如姓名和年龄,需要克隆并附加到同一个容器中。我使用以下代码实现了这一点。

html文件

<ng-template #tpl>
 <div class="form-group">
 <input type="text" id="name" class="form-control" name="name" ngModel 
 #name="ngModel">
 <input type="text" id="age" class="form-control" name="age" ngModel 
 #age="ngModel">
 <button type="Button" >Remove</button>
 </div>
</ng-template>
<div>Some element</div>
<form #myForm="ngForm" novalidate (ngSubmit)="save(myForm)">
<div #container>

</div>
<button type="submit">Submit</button>
</form>
<button (click)="gettemplate()">Add Template</button>

<pre>{{myForm.value | json}}</pre>

TS 文件

@ViewChild('container', { read: ViewContainerRef }) _vcr;
@ViewChild('tpl') tpl;

 gettemplate(){
  this._vcr.createEmbeddedView(this.tpl);
}
save(formvalue:NgForm){
   console.log(formvalue.value);
}

但是在提交表单后我没有得到表单值,而且我需要在单击删除按钮时删除克隆的元素。

【问题讨论】:

    标签: angular angular2-template clonenode


    【解决方案1】:

    这是预期行为,因为您在 ng-template 中定义的所有 ngModel 都不属于 &lt;form #myForm="ngForm",因为 Angular 具有分层依赖注入系统。

    我可以在这里为您提供两种选择:

    1) 将ng-template 移动到form 标签内

    <form #myForm="ngForm" novalidate (ngSubmit)="save(myForm)">
      <div #container></div>
      <button type="submit">Submit</button>
      <ng-template #tpl>
        <div class="form-group">
          <input type="text" id="name" class="form-control" name="name" ngModel
                 #name="ngModel">
          <input type="text" id="age" class="form-control" name="age" ngModel
                 #age="ngModel">
          <button type="Button" >Remove</button>
        </div>
      </ng-template>
    </form>
    

    Stackblirz example

    2) 在您的组件上明确提供ControlContainer

    import { NgForm, ControlContainer } from '@angular/forms';
    
    export function controlContainerFactory(component: AppComponent) {
      return component.ngForm;
    }
    @Component({
      selector: 'my-app',
      templateUrl: `./app.component.html`,
      viewProviders: [
        {
          provide: ControlContainer,
          useFactory: controlContainerFactory,
          deps: [AppComponent]
        }
      ]
    })
    export class AppComponent {
      ...   
      @ViewChild('myForm') ngForm: NgForm;
      ...
    }
    

    Stackblitz example

    另见

    【讨论】:

    • 感谢 Yurzui,我尝试了第一个选项,效果很好而且非常简单。一次更新,为了获取 Name 和 Age 字段的多个值,我将动态 id 添加到每个字段,如下所示,&lt;ng-template #tpl **let-context="context"**&gt; &lt;input type="text" id="name**{{context.session}}**" class="form-control" name="name**{{context.session}}**" ngModel #name**{{context.session}}**="ngModel"&gt; &lt;input type="text" id="age**{{context.session}}**" class="form-control" name="age**{{context.session}}**" ngModel #age**{{context.session}}**="ngModel"&gt;
    • 它有什么问题?
    • 没问题,我只是将会话val附加到元素id,以便在提交表单时获取每组元素值
    • @yurzui 你能把你的建议给这个stackoverflow.com/questions/51156152/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 2021-12-31
    相关资源
    最近更新 更多