Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngTemplateOutlet - Angular 5 nested template driven form

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

like image 398
Nijas Nizam Avatar asked Mar 05 '23 03:03

Nijas Nizam


1 Answers

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>**
like image 66
saravana va Avatar answered Mar 15 '23 16:03

saravana va