7

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

5
  • already tried this fix but no luck - stackoverflow.com/questions/39242219/…. It will be better if i can handle it with a directive as provided in the above example itself Commented Nov 5, 2018 at 12:42
  • If you set the template outlet on the container? <ng-container *ngTemplateOutlet="myTemplate"></ng-container> Docs: NgTemplateOutlet Commented Nov 5, 2018 at 12:47
  • Tried it doesn't make any difference Commented Nov 5, 2018 at 12:57
  • Have you got a solution to this? I have the same issue Commented Jul 31, 2019 at 4:30
  • workaround below from saravana va works fine. This is probably a bug in Angular ngTemplateOutlet Commented Dec 4, 2019 at 17:26

1 Answer 1

10

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>**
Sign up to request clarification or add additional context in comments.

3 Comments

how about using with loop.?
I can confirm this works. This should be set as the correct answer.
This doesn't work in terms of using primeng templates, this does - stackoverflow.com/a/57992311/2990785

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.