ngTemplateOutlet - Angular 5 nested template driven form

柔情痞子 提交于 2021-01-04 10:36:14

问题


I have a ng-template which is being passed on from one of my component and i have a placeholder to accept the passed on ng-template onto my component as shown below in ngTemplateOutlet.

<div>
<form novalidate #myForm="ngForm">
  <ng-container>
    <ng-template [ngTemplateOutlet]="myTemplate">
    </ng-template>
  </ng-container>
</form>
</div>

<!-- this template i am passing it from one of my other components -->
<ng-template #myTemplate>
  <input type="text" name="myInput" placeholder="Input"
    [(ngModel)]="inputModel" required/>
</ng-template>

The problem here is that my form('myForm') is ignoring the passed on ng-template eventhough it is marked as required. How can i make sure that my ngForm considers the passed on ng-template


回答1:


I found the answer and its very simple

Please move your code... inside your form tag

<div>
   <form novalidate #myForm="ngForm">
      <ng-container>
          <ng-template [ngTemplateOutlet]="myTemplate">
          </ng-template>
      </ng-container>
   </div>

      <!-- this template i am passing it from one of my other components -->
   <ng-template #myTemplate>
      <input type="text" name="myInput" placeholder="Input"
       [(ngModel)]="inputModel" required/>
     </ng-template>

  **</form>**


来源:https://stackoverflow.com/questions/53154648/ngtemplateoutlet-angular-5-nested-template-driven-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!