No ability to check if <ng-content> was provided to the component
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:
Current behavior
We can use ng-content
in our components. But we don’t have any options out of the box to check if anything is passed as ng-content
or not. Actually, was anything passed between >
and </
of our component usage in HTML.
We can do this by adding some wrap component (and if I understood correctly you can not use stuff like ng-container
there). Like div. And you put ng-content to it. You can get ref for this div and check if this div has children. If yes - ng-content was provided, otherwise - no ng-content. Huge minus of this approach is you have to create some additional entity to check it.
Expected behavior
We can create some util function for that. You will have ability to just call that util and detect if ng-content
was passed.
Minimal reproduction of the problem with instructions
This is not a bug. This example shows us how we can do this at the moment. https://stackblitz.com/edit/angular-xdirt8
What is the motivation / use case for changing the behavior?
We will not be forced to add some wrappers and it will be easier to check this stuff.
Environment
Angular version: 6.0.0
Others:
I tried to ask it on stackoverflow and I am not only one person who is wondered how to do this.
https://stackoverflow.com/questions/52479608/how-to-check-if-ng-content-was-passed-into-the-component https://stackoverflow.com/questions/35107211/in-angular-2-how-to-check-whether-ng-content-is-empty https://stackoverflow.com/questions/38692881/how-to-check-whether-ng-content-exists
I am not supa-pro in vuejs, but seems to me it has this feature out of the box: https://vuejs.org/v2/api/?#vm-slots
Issue Analytics
- State:
- Created 5 years ago
- Reactions:95
- Comments:22 (6 by maintainers)
Top GitHub Comments
Still not a true solution for this ? Seem to be a basic usage… absolutely required when building a library !
This is a basic feature that should exist. Current solutions of counting child nodes are explicitly web-based and do not reflect the fact Angular can be used in many other scenarios, and has been built from the ground up to be able to do so. I am using NativeScript Angular, for example, and css empty selector or counting child nodes won’t necessarily suffice - this should be included as standard.