1

I've been studying Angular's lifecycle hooks while looking for a way to know when and which child components are loaded.

I see that ngAfterViewInit()

"Responds after Angular initializes the component's views and child views."

Since ngAfterViewInit knows about the children, how could I get their identifying information as they (or after) they initialize?

Something like this pseudo code:

// ngAfterViewInit() - Respond after Angular initializes the component's views and child views.
export class AppComponent implements AfterViewInit {
    ngAfterViewInit() {
        console.log(‘Children should be loaded');
        // loop through ngAfterViewInit
        querySelector('body').classList.add(ngAfterViewInit[i].componentName);
    }
}

sidenote: the goal is to add component names or selector names to <body>'s class list.

Ben Racicot
  • 5,332
  • 12
  • 66
  • 130
  • What's this? identifying information – onetwo12 Jul 22 '17 at 23:08
  • As far as I know, in order to have components communicate with each other, you want to use `@Output` properties. When the child component initializes, you can broadcast an event that the parent component is subscribed to. – Jake Smith Jul 22 '17 at 23:12
  • But considering what you are trying to do, I wonder if you could name your child components and refer to them straight in your markup. Could you provide some code snippets of what you are trying to do? – Jake Smith Jul 22 '17 at 23:13
  • Hey @JakeSmith Thats where I started, but it turns out that the lifecycle hooks do that without using `@input` and `@output`. I'd rather not setup I/O on every component in my app just to get the names if I don't have to. (I'll update the question with some psuedo code) – Ben Racicot Jul 22 '17 at 23:15
  • 1
    Gotcha. I think I could help better if I knew a little more of what your code looked like. If the components are not dynamically loaded in, you should be able to do something like `` and then on your body tag: `...` – Jake Smith Jul 22 '17 at 23:17
  • Oh wow, like if each component class had a method that simply returned it's name as a string and trigger it in the body? Do I understand that right? It would be 1 step better than I/O setup but maybe we can get it cleaner still? – Ben Racicot Jul 22 '17 at 23:18

0 Answers0