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

229
Views
Componente interno de acceso angular de ng-template

Digamos que tenemos un componente llamado TopComponent con una plantilla como esta:

 <ng-template #top> <inner-selector #inner></inner-selector> </ng-template>

Su código es simple:

 //... imports ... @Component({ // selector, styles etc }) export class TopComponent implements AfterViewInit { @ViewChild('top') topComp : NgbModal; // NgbModal is a type provided by some 3rd-party library // I can't modify it ngAfterViewInit() { // let's access the #inner component from here } }

Puedo acceder al componente #top usando este código:

 @ViewChild('top') topComponent: TopComponent;

¿Cómo puedo acceder al componente #inner desde la misma clase?

Intenté usar @ContentChild('inner') pero sigo undefined .

PD. Sé que puedo crear otro componente, usarlo en lugar de <ng-template> y acceder a todos los elementos secundarios necesarios desde él. Pero, ¿se puede evitar esto de alguna manera?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Usar descendientes @ViewChild('inner', {descendants: true}) inner: InnerSelector;

aquí está el código de relleno: https://plnkr.co/edit/MtVhHuAtPXZzC7GLOidF?p=preview

 @Component({ selector: 'inner-selector', template:'inner-selector here' }) export class InnerSelector { myVar = 0; } @Component({ selector: 'my-app', template: ` <div> <ng-template #top> <inner-selector #inner></inner-selector> </ng-template> <ng-container [ngTemplateOutlet]="top"></ng-container> </div> `, }) export class App { @ViewChild('inner', {descendants: true}) inner: InnerSelector; constructor() { this.name = `Angular! v${VERSION.full}` } ngAfterViewInit() { console.log(this.inner); } }
about 4 years ago · Santiago Trujillo 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!