question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No ability to check if <ng-content> was provided to the component

See original GitHub issue

I’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:open
  • Created 5 years ago
  • Reactions:95
  • Comments:22 (6 by maintainers)

github_iconTop GitHub Comments

53reactions
imike57commented, Apr 3, 2019

Still not a true solution for this ? Seem to be a basic usage… absolutely required when building a library !

51reactions
joshcomleycommented, Oct 20, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check whether <ng-content> is empty? (in Angular 2+ ...
There is another technique to accomplish the default content if not provided from the implementation component by using *ngTemplateOutlet ...
Read more >
Angular ng-content and Content Projection: A Complete Guide ...
We are going to see how content projection works, when to use it and why, and how it can improve a lot the...
Read more >
Everything you need to know about ng-template, ng-content ...
If any one of them is not provided, our component will show the default template in its place. Thus, creating a highly customized...
Read more >
Testing Components – Testing Angular
... for common Component testing tasks. Components are the power houses of an Angular application. ... The Component is not fully rendered.
Read more >
Hierarchical injectors - Angular
Provide services with the providedIn property of @Injectable() as follows: ... You can do this to configure a non-default provider of a service...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found