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

349
Views
Angular: ¿cómo acceder al componente secundario desde el componente abuelo?

Tengo un componente A que tiene un contenido ng. Después de esto, tengo un componente B dentro del componente A. El componente B tiene un componente C en su plantilla.

plantilla del componente A:

 <ng-content></ng-content>

plantilla de componente B:

 <componentC></componentC>

plantilla de componente C:

 <div> <span>test</span> <div>

En mi html tengo esta solución final:

 <componentA> <componentB></componentB> </componentA>

Usaría un viewChild dentro del componente A para encontrar el componente C que está dentro del componente B pero devuelve indefinido

 @ViewChild(ComponentC) componentC: ComponentC

¿Cómo puedo solucionar este problema?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

En servicio:

 private sharedValue = new BehaviorSubject('Initial value') currentSharedValue = this.sharedValue.asObservable() changeSharedValue(newValue: string) { this.sharedValue.next(newValue) }

Si no desea establecer un valor inicial, puede usar un Asunto regular y si desea realizar un seguimiento del historial, un ReplaySubject.

Datos de cambio de componente:

 constructor( private dataService: DataService ) { } changeSharedData(): void { this.dataService.changeSharedValue('New Shared Data') }

Componente leyendo datos compartidos:

 value = 'Old Value' subscription!: Subscription; constructor( private dataService: DataService ) { } ngOnInit() { this.subscription = this.dataService.currentSharedValue.subscribe(newValue => { this.value = newValue }) } ngOnDestroy() { this.subscription.unsubscribe() }
over 4 years ago · Santiago Trujillo Report

0

Puede hacerlo de diferentes maneras, ¡deje que el componente padre apunte al hijo!

Entonces, suponiendo que ComponentA es padre de ComponentB dentro de su HTML

 <ComponentA><ComponentB></ComponentB></ComponentA>

Entonces, en ComponentA ts define una variable pública de ComponentB

 myComponent: ComponentB;

En el constructor de ComponentB, inyecte ComponentA

constructor(parent:ComponentA) { parent.myComponent = this; }

ahora el ComponentA principal puede acceder al componente B secundario

over 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!