Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda