title: Angular 12: ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement')
body: <p>I have phone number input field in which I have attached prefixes dropdown. Below is code for that.</p>
  <p><strong>HTML code in modal</strong></p>
  <pre><code>&lt;div
    class=&quot;phone&quot;
    [ngClass]=&quot;{ 'error_border': submitted &amp;&amp; f.phoneNumber.errors }&quot;
  &gt;
    &lt;div class=&quot;phone__select&quot;&gt;
                  &lt;span class=&quot;phone__prefix&quot; (click)=&quot;openPrefixes()&quot;
                  &gt;{{ selectedPrefix }}&lt;/span
                  &gt;
      &lt;ul #prefixes class=&quot;prefixes&quot;&gt;
        &lt;li
          *ngFor=&quot;let prefix of availablePrefixes&quot;
          class=&quot;prefix&quot;
          (click)=&quot;prefixChange(prefix)&quot;
        &gt;
          {{ prefix }}
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    &lt;input
      class=&quot;phone__input form-control&quot;
      maxlength=&quot;13&quot;
      type=&quot;text&quot;
      pattern=&quot;^(?!0+$)\d{10,}$&quot;
      formControlName=&quot;phoneNumber&quot;
    /&gt;
  &lt;/div&gt;
  </code></pre>
  <p><strong>TS</strong></p>
  <pre><code> @ViewChild(&quot;prefixes&quot;) prefixes!: ElementRef;
    selectedPrefix = &quot;+43&quot;;
    availablePrefixes = [&quot;+43&quot;, &quot;+49&quot;];
    
    prefixChange(prefix: string): void {
      this.selectedPrefix = prefix;
      this.closePrefixes();
    }
  
    openPrefixes(): void {
      this.renderer.addClass(this.prefixes.nativeElement, &quot;active&quot;);
    }
  
    closePrefixes(): void {
      this.renderer.removeClass(this.prefixes.nativeElement, &quot;active&quot;);
    }
  </code></pre>
  <p>Above code works fine on page but its giving error like <code>ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement')</code> when I use above code on modal.</p>
  <p>When modal opens prefix dropdown is not working and showing that error.</p>
  <p>How should I resolve above error? Is there any alternative way to do this (e.g. using <code>ngModal</code>) ?</p>
  <p>Please help and guide.</p>