New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No ability to check if <ng-content> was provided to the component #26083
Comments
|
Any ideas on this? Maybe I am missing something and there is something what I can use? |
|
@sharikovvladislav ... you can always mark the transcluded content and later use |
|
@mlc-mlapis I know this. This is not what I ask. I have a bit different situation. I don't know what will be projected to my component. And I don't want everybody to pass something to my component with |
|
@sharikovvladislav ... but it is also your API as many other things you abstract.
If somebody has access to the source code, anything can be passed. |
|
@mlc-mlapis It is not explicit API at all.
It is not good abstraction if you need to read source codes of some stuff to use it. My problem can be solved by your way or by my suggestion (well, not my, I think I found this somewhere about half a year ago), but it is not as explicit and as easy as using simple util function (or another simple API). |
|
@sharikovvladislav ... yep, I know. Something similar as injecting via DI would nice to have but actually I think that you don't have anything else. There are plans to re-write transclusion functionality to have more flexible options but it's planned to post Ivy era. |
I didn't find anything else too. Thank you very much for your suggestions. |
|
Similar solution to yours: For now the best way is probably to use templates instead of projections (not perfect though). |
|
The problem is that if there is an And that's is going to be counted as a node, so |
|
So if you are projecting any *ngIf inside a You can do this |
|
@leomendoza123 ... sure, it would work, but this is much more simpler way: #26083 (comment) |
|
Also, there's a |
ng-content was provided to the component|
Still not a true solution for this ? Seem to be a basic usage... absolutely required when building a library ! |
|
Consider looking into the |
No, it's not simpler: in your solution, you require any user of the component to add a div with a magic id. In the solution of @leomendoza123 you put the complexity in the reusable component. Take this with a grain of salt, but when designing a library, it doesn't really matter how much complexity you have in the implementation. The only thing that matters is ease of use (given the same feature set). |
|
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. |
|
I agree with @joshcomley. One shouldn't have to write complex angular code or use some hacky stuff to figure out if any content is being passed in. It should be a simple boolean variable available to the template. React may be of some inspiration. Commenting to push this issue up and also sharing that this is a much-needed feature. |
|
This can be particularly painful in case you would like to keep the html semantics and styling internal to the component but only accept text and/or children. Like: <h3 class="headline"><ng-content select="headline"></ng-content></h3>I you'd want to keep |
|
From what I understand, the request here is to have something similar as what was done in AngularJS where you could query the $transclude service with
I have used this a number of times in AngularJS to add an expand button to a panel. I definitely miss this in Angular. |


I'm submitting a...
Current behavior
We can use
ng-contentin our components. But we don't have any options out of the box to check if anything is passed asng-contentor 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-containerthere). 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-contentwas 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
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
The text was updated successfully, but these errors were encountered: