open ng-template in ngOnInit function not working

1.4k views Asked by At

I created one ng-template with two text fields. I am trying to open this template popup when my screen loads. But when I pass my id its not working.

<ng-template #contentreset let-c="close" let-d="dismiss">
                <div class="modal-header">
                    <p class="modal-title" style="color:black;font-weight: bold;font-size:14px;">Reset password</p>
                    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                 <
                </div>

ngOnInit() {
 this.modalService.open(contentreset,{ centered: true }); //here modalservice is NgbModal

});

I tried passing the value from other button click but it doesnt worked. How to pass the ng template id while the page loads

2

There are 2 answers

0
Ganesh On

before using ngTemplate we need to create it's tempateRef in component, so try like this

 @ViewChild('contentreset ') contentreset : TemplateRef<any>;

call this template in ngAfterViewInit() for more info have look here

 ngAfterViewInit() {
     this.modalService.open(this.contentreset,{ centered: true }); //here modalservice is NgbModal
 });
2
Amrit On

your view is not loaded try it on

ngAfterViewInit() {

} life cycle of component

like this:

ngAfterViewInit() {
 this.modalService.open(contentreset,{ centered: true }); //here modalservice is NgbModal

});

More on LIFE_CYCLE_HOOKS