简体   繁体   中英

How to access TemplateRef on the component of Angular?

Component TS

ngOnInit() {
    if(somecondition)
        // This is the line of code that wont work
        this.openModal(#tempName);
}

Component HTML

<ng-template #tempName>
    I got some content here
</ng-template>

this.openModal(#tempName) -> How do i get access to the ngTemplate tempName here?

Flyn you put in your code

@ViewChild('tempName') mymodal: ElementRef;
//You can NOT use this.mymodal at ngInit, the early time you can use is in ngAfterViewInit
ngAfterViewInit()
{
 if (somecondition)
   this.openModal(mymodal);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM