title: mat-autocomplete inside an ng-template
body: <p>I have a component that should choose beetween two mat-autocomplete depending an input value.</p>
  
  <p>Both mat-autocomplete works without the ngIf, but when include it they stop working, I see "[object Object]" and it crashes when clicking:</p>
  
  <pre><code>Error: Attempting to open an undefined instance of `mat-autocomplete`. Make sure that the id passed to the `matAutocomplete` is correct and that you're attempting to open it after the ngAfterContentInit hook.
  
  
  &lt;div *ngIf="grouped;then grouped else notGrouped"&gt;
  &lt;/div&gt;
  &lt;ng-template #grouped&gt;
     &lt;mat-autocomplete  #auto="matAutocomplete" [displayWith]="inputDisplay" &gt;
        &lt;mat-optgroup *ngFor="let group of groupedItems" [label]="group.searchString"&gt;
          &lt;mat-option *ngFor="let item of group.items" [value]="item"&gt;
            {{ item.search }}
          &lt;/mat-option&gt;
       &lt;/mat-optgroup&gt;
    &lt;/mat-autocomplete&gt;
  &lt;/ng-template&gt; 
  &lt;ng-template #notGrouped&gt; 
     &lt;mat-autocomplete *ngIf="!grouped" #auto2="matAutocomplete" [displayWith]="inputDisplay"&gt;
        &lt;mat-option *ngFor="let item of filteredItems" [value]="item"&gt;
              {{ item.search }}
         &lt;/mat-option&gt;
     &lt;/mat-autocomplete&gt;
  &lt;/ng-template&gt;
  </code></pre>
  
  <p>Is it not possible to include mat-autocomplete in an ngIf?</p>