Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Angular: Add class to html element inside a CDK Portal

I have an app with Angular12 and the Angular Material CDK.

I am adding a template into a CDK Portal, the template is this:

<ng-template #templatePortalContent>
     <div class="editable" #newFormField>
         stuff            
     </div>
</ng-template>

I need to add a class dynamically to the #newFormField item and can't figure out how to access it. Normally, if this was a regular ng-template such as #addressBlock I can use

 @ViewChild('addressBlock') addressBlock: ElementRef;
 this.addressBlock.nativeElement.classList.add('editingActive');

However, this does not work and throws an error

 @ViewChild('newFormField') newFormField: ElementRef;
 this.newFormField.nativeElement.classList.add('editingActive');

I figure because the newFormField is inside the #templatePortalContent template which was inserted into the portal.

How do I refer to the inner element?

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

That template will be able to access component properties from where it's declared. The ViewChild will also be accessible once the template is rendered (I'm pretty sure ๐Ÿ‘€)

<ng-template #templatePortalContent>
     <div [ngClass]="editableClass" #newFormField>
         stuff            
     </div>
</ng-template>

// In the component
public editableClass = 'editable' 
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!