title: Angula6 ViewChild in @ng-bootstrap/ng-bootstrap NgbModalModule modal returns undefined
body: <p>I am successfully using ng bootstrap and specifically the modal module to show a modal form. It is a contact form that has email and message input fields and a submit button. The ngbootstrap module I am using is <a href="https://ng-bootstrap.github.io/#/components/modal/examples" rel="nofollow noreferrer">https://ng-bootstrap.github.io/#/components/modal/examples</a> this shows fine. What I am trying to do is that when user clicks on the 'send' button (see template code below) the button will become disabled and say 'sending...'. In other words I'm trying to manipulate the  properties view the modal component. </p>
  
  <p>I have already looked at the following questions but none of the suggestions work: </p>
  
  <p><a href="https://stackoverflow.com/questions/34947154/angular-2-viewchild-annotation-returns-undefined">Angular 2 @ViewChild annotation returns undefined</a>
  <a href="https://stackoverflow.com/questions/39908967/how-to-get-reference-of-the-component-associated-with-elementref-in-angular-2/39909203#39909203">How to get reference of the component associated with ElementRef in Angular 2</a></p>
  
  <p>However no matter what, I get 'undefined' when I try to use in my component</p>
  
  <pre><code>@ViewChild('submitButton') submitButton: ElementRef;
  </code></pre>
  
  <p>along with this html in my template</p>
  
  <pre><code>&lt;button #submitButton
     id="submitButton"
     class="btn btn-success w-100"
     [disabled]="!contactForm.valid"
  &gt;Send&lt;/button&gt;
  </code></pre>
  
  <p>Here is the full component code (the place where I am trying to access the submitButton is in method <code>onSubmit()</code>):</p>
  
  <pre><code>import {
      Component,
      OnInit,
      Renderer2,
      ViewChild,
      ElementRef,
      AfterViewInit
  } from '@angular/core';
  import { FormGroup, FormControl, Validators } from '@angular/forms';
  import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
  
  import { ContactService } from '../../services/contact.service';
  
  @Component({
      selector: 'app-contact-modal',
      templateUrl: './contact-modal.component.html'
  })
  export class ContactModalComponent implements OnInit, AfterViewInit {
  
      closeResult: string;
      contactForm: FormGroup;
      //@ViewChild('submitButton') submitButton;
      @ViewChild('submitButton') submitButton: ElementRef;
  
      constructor(
          private modalService: NgbModal,
          private contactService: ContactService,
          private renderer: Renderer2
      ) { }
  
      ngOnInit() {
          this.initForm();
      }
  
      ngAfterViewInit() {
          console.log(this.submitButton);
      }
  
      private getDismissReason(reason: any): string {
          if (reason === ModalDismissReasons.ESC) {
            return 'by pressing ESC';
          } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
            return 'by clicking on a backdrop';
          } else {
            return  `with: ${reason}`;
          }
      }
  
      private initForm() {
          const email = '';
          const message = '';
  
          this.contactForm = new FormGroup({
              email: new FormControl(null, Validators.email),
              message: new FormControl(null, Validators.required)
          });
      }
  
      onSubmit() {
  
          const email = this.contactForm.value.email;
          const message = this.contactForm.value.message;
  
          // at this point this.submitButton is UNDEFINED
          console.log(this.submitButton);
  
          //this.submitButton.nativeElement.style.backgroundColor = 'black';
  
          this.contactService.sendContactRequest(email, message, (submitSuccess: boolean) =&gt; {
              if (submitSuccess) {
  
                  console.log('SUCCESS UPDATE UI');
                  this.contactForm.value.email = '';
                  this.contactForm.value.message = '';
  
              } else {
                  console.log('ERROR update UI');
              }
          });
      }
  
      open(content) {
          console.log('in open func');
          this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'})
          .result
          .then(
              (result) =&gt; {
                  console.log(`Closed with: ${result}`);
              },
              (reason) =&gt; {
                  console.log(`Dismissed ${this.getDismissReason(reason)}`);
              }
          );
      }
  }
  </code></pre>
  
  <p>Here is my full template:</p>
  
  <pre><code>&lt;a
      class="nav-link"
      (click)="open(content)"
  &gt;&lt;i class="fas fa-envelope"&gt;&lt;/i&gt;&lt;/a&gt;
  
  &lt;ng-template #content let-c="close" let-d="dismiss"&gt;
  
      &lt;div class="modal-header"&gt;
          &lt;h4 class="modal-title" id="modal-basic-title"&gt;Contact Us&lt;/h4&gt;
          &lt;button type="button" class="close" aria-label="Close" (click)="c()"&gt;
            &lt;i class="fas fa-times"&gt;&lt;/i&gt;
          &lt;/button&gt;
      &lt;/div&gt;
  
      &lt;div class="modal-body"&gt;
  
          &lt;form [formGroup]="contactForm" (ngSubmit)="onSubmit()"&gt;
  
              &lt;div class="row justify-content-center"&gt;
  
                  &lt;div class="col-md-12 col-sm-12"&gt;
                      &lt;div class="form-group"&gt;
                          &lt;label for="email"&gt;Email&lt;/label&gt;
                          &lt;input
                              type="text"
                              class="form-control"
                              id="email"
                              formControlName="email"
                          &gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
              &lt;/div&gt;
  
              &lt;div class="row justify-content-center"&gt;
  
                  &lt;div class="col-md-12 col-sm-12"&gt;
                      &lt;div class="form-group"&gt;
                          &lt;label for="message"&gt;Message&lt;/label&gt;
                          &lt;textarea
                              type="text"
                              class="form-control"
                              id="message"
                              formControlName="message"
                              rows="6"
                          &gt;&lt;/textarea&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
              &lt;/div&gt;
  
              &lt;div class="row justify-content-center"&gt;
  
                  &lt;div class="col-md-12 col-sm-12"&gt;
                      &lt;button #submitButton
                          id="submitButton"
                          class="btn btn-success w-100"
                          [disabled]="!contactForm.valid"
                      &gt;Send&lt;/button&gt;
                  &lt;/div&gt;
  
              &lt;/div&gt;
  
              &lt;div class="row"&gt;
                  &lt;div class="col-md-12 col-sm-12"&gt;
                      &lt;div *ngIf="sentMessage" class="alert alert-success" role="alert"&gt;
                          {{ sentMessage }}
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
          &lt;/form&gt;
  
      &lt;/div&gt;
  
  &lt;/ng-template&gt;
  </code></pre>
  
  <p>Any help is greatly appreciated.</p>