Visitar URL original
No ability to check if <ng-content> was provided to the component · Issue #26083 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20220627165141/https://github.com/angular/angular/issues/26083
Skip to content
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

Open
sharikovvladislav opened this issue Sep 24, 2018 · 19 comments
Open

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

sharikovvladislav opened this issue Sep 24, 2018 · 19 comments
Labels
comp: core core: content projection feature under consideration
Milestone

Comments

@sharikovvladislav
Copy link

@sharikovvladislav sharikovvladislav commented Sep 24, 2018 •

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

@sharikovvladislav
Copy link
Author

@sharikovvladislav sharikovvladislav commented Sep 24, 2018

Any ideas on this? Maybe I am missing something and there is something what I can use?

@mlc-mlapis
Copy link
Contributor

@mlc-mlapis mlc-mlapis commented Sep 24, 2018

@sharikovvladislav ... you can always mark the transcluded content and later use @ContentChild('mark') as ...

<my-parent>
   <my-child #mark>
      ...
   <my-child>
</my-component>

@sharikovvladislav
Copy link
Author

@sharikovvladislav sharikovvladislav commented Sep 24, 2018 •

@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 mark ref. It requires some additional arrangements in team. Not sure this is true way. But yes, this will work.

@mlc-mlapis
Copy link
Contributor

@mlc-mlapis mlc-mlapis commented Sep 24, 2018

@sharikovvladislav ... but it is also your API as many other things you abstract.

I don't know what will be projected to my component.

If somebody has access to the source code, anything can be passed.

@sharikovvladislav
Copy link
Author

@sharikovvladislav sharikovvladislav commented Sep 24, 2018

@mlc-mlapis It is not explicit API at all.

If somebody has access to the source code, anything can be passed.

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).

@mlc-mlapis
Copy link
Contributor

@mlc-mlapis mlc-mlapis commented Sep 24, 2018

@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.

@sharikovvladislav
Copy link
Author

@sharikovvladislav sharikovvladislav commented Sep 24, 2018 •

@mlc-mlapis

I think that you don't have anything else

I didn't find anything else too. Thank you very much for your suggestions.

@kara kara added the comp: core label Sep 24, 2018
@ngbot ngbot bot added this to the needsTriage milestone Sep 24, 2018
@kemsky
Copy link

@kemsky kemsky commented Sep 27, 2018

Similar solution to yours:

<ng-container><span #ref><ng-content></ng-content></span></ng-container>
<ng-container *ngIf="ref.childNodes.length == 0">{{name || title}}</ng-container>

For now the best way is probably to use templates instead of projections (not perfect though).

@alxhub alxhub added the feature label Sep 27, 2018
@ngbot ngbot bot removed this from the needsTriage milestone Sep 27, 2018
@ngbot ngbot bot added this to the Backlog milestone Sep 27, 2018
@leomendoza123
Copy link

@leomendoza123 leomendoza123 commented Jan 31, 2019 •

The problem is that if there is an *ngIf evaluated to false inside the ng-content, Angular is going to add a comment (at least on development mode) like

bindings={
  "ng-reflect-ng-if": null
}-->

And that's is going to be counted as a node, so *ngIf="ref.childNodes.length == 0" is not going to work

@leomendoza123
Copy link

@leomendoza123 leomendoza123 commented Jan 31, 2019

So if you are projecting any *ngIf inside a ng-content and you want to check if the projection is empty.

You can do this

<div #contentProjection>
   <ng-content></ng-content>
</div>`
<div *ngIf="hasContentProjection()"> NO CONTENT PROJECTION</div>
@ViewChild('contentProjection') contentProjection: ElementRef

hasContentProjection() {
    const detailContentProjection: NodeList = (this.contentProjection
      .nativeElement as HTMLElement).childNodes
    for (let x = 0; x < detailContentProjection.length; x++) {
      console.log(detailContentProjection.item(x).nodeType)
      if (detailContentProjection.item(x).nodeType !== 8) {
        return true // nodeType == 8 means is a comment
      }
    }
 }

@mlc-mlapis
Copy link
Contributor

@mlc-mlapis mlc-mlapis commented Jan 31, 2019

@leomendoza123 ... sure, it would work, but this is much more simpler way: #26083 (comment)

@trotyl
Copy link
Contributor

@trotyl trotyl commented Feb 1, 2019

Also, there's a cdkObserveContent directive in the CDK should be able to help with this situation: https://material.angular.io/cdk/observers/overview

@pkozlowski-opensource pkozlowski-opensource changed the title No ability to check if ng-content was provided to the component No ability to check if <ng-content> was provided to the component Feb 1, 2019
@imike57
Copy link

@imike57 imike57 commented Apr 3, 2019

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

@ScottMGerstl
Copy link

@ScottMGerstl ScottMGerstl commented Jul 29, 2019

Consider looking into the :empty CSS Selector

@alberto-chiesa
Copy link

@alberto-chiesa alberto-chiesa commented Aug 20, 2019

@leomendoza123 ... sure, it would work, but this is much more simpler way: #26083 (comment)

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).

@joshcomley
Copy link

@joshcomley joshcomley commented 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.

@stepancar
Copy link

@stepancar stepancar commented Nov 5, 2019

@sharikovvladislav
https://medium.com/@istepancar/how-to-avoid-render-wrapper-for-ng-content-when-it-not-passed-in-dartangular-d89cb3b026a

@Adit-COCO-Garg
Copy link

@Adit-COCO-Garg Adit-COCO-Garg commented Jul 22, 2021

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.

@alxhub alxhub added this to Inbox in Feature Requests via automation Sep 23, 2021
@alxhub alxhub moved this from Inbox to Proposed Projects in Feature Requests Sep 23, 2021
@klemenoslaj
Copy link
Contributor

@klemenoslaj klemenoslaj commented Nov 9, 2021

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 h3 and class headline internal for the component, you can end up with empty h3 element which can cause accessibility issues.

@niki-lijnen
Copy link

@niki-lijnen niki-lijnen commented Dec 29, 2021 •

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

$transclude.isSlotFilled('mySlotName')

I have used this a number of times in AngularJS to add an expand button to a panel. I definitely miss this in Angular.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp: core core: content projection feature under consideration
Projects
Feature Requests
Proposed Projects
Development

No branches or pull requests