I have successfully integrated ng bootstrap into my project, specifically utilizing the modal module to display a contact form. The form includes input fields for email and message, as well as a submit button. You can find the ngbootstrap module I am using here. Everything is displaying correctly. My goal now is to implement a feature where, upon clicking the 'send' button (refer to the template code below), the button will become disabled and show 'sending...' This involves manipulating the properties of the modal component.
I've reviewed several related questions on this topic without success:
Angular 2 @ViewChild annotation returns undefined How to get reference of the component associated with ElementRef in Angular 2
Despite these efforts, I consistently receive 'undefined' when attempting to use it in my component.
@ViewChild('submitButton') submitButton: ElementRef;
Included in my template html is the following:
<button #submitButton
id="submitButton"
class="btn btn-success w-100"
[disabled]="!contactForm.valid"
>Send</button>
Below is the complete component code snippet, where I'm trying to access the submitButton within the onSubmit() method:
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) => {
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) => {
console.log(`Closed with: ${result}`);
},
(reason) => {
console.log(`Dismissed ${this.getDismissReason(reason)}`);
}
);
}
}
Here is my full template markup:
<a
class="nav-link"
(click)="open(content)"
><i class="fas fa-envelope"></i></a>
<ng-template #content let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">Contact Us</h4>
<button type="button" class="close" aria-label="Close" (click)="c()">
<i class="fas fa-times"></i>
</button>
</div>
<div class="modal-body">
<form [formGroup]="contactForm" (ngSubmit)="onSubmit()">
<div class="row justify-content-center">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<label for="email">Email</label>
<input
type="text"
class="form-control"
id="email"
formControlName="email"
>
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<label for="message">Message</label>
<textarea
type="text"
class="form-control"
id="message"
formControlName="message"
rows="6"
></textarea>
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-12 col-sm-12">
<button #submitButton
id="submitButton"
class="btn btn-success w-100"
[disabled]="!contactForm.valid"
>Send</button>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div *ngIf="sentMessage" class="alert alert-success" role="alert">
{{ sentMessage }}
</div>
</div>
</div>
</form>
</div>
</ng-template>
Any assistance on this matter would be highly appreciated.