title: how to focus on an input in Angular
body: <p>This should be simple but I can't find a solution, the closest answer i can find on SO is this: <a href="https://stackoverflow.com/questions/41190075/how-do-i-programmatically-set-focus-to-dynamically-created-formcontrol-in-angula">How do I programmatically set focus to dynamically created FormControl in Angular2</a></p>
  
  <p>However this is way more convoluted that my requirements. Mine are simple. I have an input in the template like so:</p>
  
  <pre><code>&lt;div *ngIf="someValue"&gt;    
      &lt;input class="form-control" #swiper formControlName="swipe" placeholder="Swipe"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>(Note, it's in an ngx-boostrap form-control group but I'm pretty sure this should not matter)</p>
  
  <p>And I have a button that when clicked calls the following function:</p>
  
  <pre><code>onClick() {
      this.renderer.invokeElementMethod(this.swiper.nativeElement, 'focus')
  }
  </code></pre>
  
  <p>The goal is that when the button is clicked, my input element gets focus.  here is the rest of my component (the relevant parts):</p>
  
  <pre><code>import { Component, ElementRef, ViewChildren, Renderer } from '@angular/core';
  import { MyService } from wherever/my/service/is
  
  export class MyClass {
      @ViewChildren('swiper') input: ElementRef;
      someValue: any = false;
  
      constructor(private renderer: Renderer, service: MyService) {
         this.service.getSomeData().subscribe(data =&gt; {
            this.someValue = true;
         }
      }
  
      onClick() {
          this.renderer.invokeElementMethod(this.swiper.nativeElement, 'focus');
      }
  }
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>ERROR TypeError: Cannot read property 'focus' of undefined
  at RendererAdapter.invokeElementMethod (core.js:12032)
  etc...
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>(Angular 5 btw)</p>
  
  <p>Note, I edited the code to more accurately reflect what is actually going on, I think there may be some synchronization issue.</p>