I have a component that includes a modal. The modal is wrapped in a <ng-template>
tag.
<ng-template #modalm let-modal>
...
<button type="button" (click)="onSubmit()" class="btn btn-primary">OK</button>
...
</ng-template>
This allows me to open and close the modal from my component.ts file.
constructor(private modalService: NgbModal,
private activeModal: NgbActiveModal)
...
public openModal(modalm: TemplateRef<any>){
this.activeModal = this.modalService.open(modalm);
const btn = modalm.elementRef.nativeElement.querySelector('.btn-primary');
}
I would like to access the button element and set a property on it. I am following the accepted answer in How do I find Element inside TemplateRef, calling querySelector
off of modalm.elementRef.nativeElement
. This gives me an error:
TypeError: modalm.elementRef.nativeElement.querySelector is not a function
Why am I not able to call querySelector
in this case?
I understand that I have to use the type TemplateRef due to the modal being wrapped in ng-template
. What is the correct syntax for accessing an element in the modal?