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.

@ContentChildren cannot find children whose parent has a directive like ngClass, ngStyle

See original GitHub issue

(Updated to reflect discussion below)

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior When @ContentChildren is used to get a QueryList of elements inside the <ng-content></ng-content>, it fails to find children with a directive like [ngClass]="..." or [ngStyle]="..." in their parent. Once the parent has no more directive, the problem goes away and those children are found. Using {descendants: true} in @ContentChildren seems to work around this problem.

Expected behavior Those children should be found.

Minimal reproduction of the problem with instructions

Minimal demo: http://plnkr.co/edit/0nPxOa7t0FLDY98PFfnW?p=preview

  • Please click the two buttons and check console log output
  • One of them finds (the one at the ng-content level) 3 instances of the SomeDirective
  • The other one finds (the one at within the ng-content) 9 instances of the SomeDirective

What is the motivation / use case for changing the behavior?

This is an inconsistent behavior and it causes confusion.

Please tell us about your environment:

Windows 8.1, IntelliJ, npm

  • Angular version: 2.4.0
  • Browser: tried on Chrome 56 | IE 11
  • Language: all

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

7reactions
pkozlowski-opensourcecommented, Jan 11, 2018

OK, so after trying to come up with a sensible documentation for the current behaviour @mhevery and I believe that the way things work correctly is confusing to the point that we can label it as a “bug” and propose a simpler system.

The current working proposal is to change descendants: false to follow the following rule: only query direct children (in the sense of elements in a template) when descendants: false is specified. Some examples give a @ContentChildren(Tab, {descendants: false}) from the <tabset> component:

<tabset>
  <tab>...</tab>
  <tab>...</tab>
</tabset>

In the above example the query would pick up 2 <tab>s.

<tabset>
    <div>
      <tab>...</tab>
      <tab>...</tab>
    </div>
</tabset>

In the above example the query would pick up 0 <tab>s (no tabs selected since no <tab> element is a direct child of <tabset>).

<tabset>
    <div>
      <tab>...</tab>
    </div>
    <tab>...</tab>
</tabset>

In the above example the query would pick up 1 <tab>s (only the second <tab> element is a direct child of <tabset>).

The above proposal will make rules pretty clear and easy to follow. Unfortunately it will be a breaking change, technically speaking. But at present we believe that it is more of a bug-fix rather than change of the expected behaviour.

However, if you’ve got a valid use-case that will be breaking after implementation of the above proposal, please speak up! @danielcheng825 I would love to know what is your real-life use-case for having an element with ngClass above the content queried for.

2reactions
ericmartinezrcommented, Feb 27, 2017

https://angular.io/docs/ts/latest/api/core/index/ContentChildren-decorator.html

descendants - include only direct children or all descendants.

@ContentChildren(Type, {descendants:true})

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular 2 | can't get child directives with @ContentChildren
There was a bug (fixed in 2.0.1) that @ContentChildren() didn't search descendants by default. You can work around with setting it ...
Read more >
ViewChildren & ContentChildren • Angular - codecraft.tv
This decorator tells Angular how to find the child component that we want to bind to this property. A @ViewChild decorator means, search...
Read more >
Template syntax - Angular
Angular executes the expression and assigns it to a property of a binding target; the target could be an HTML element, a component,...
Read more >
angular/core
Parameter decorator for a directive constructor that designates a host-element attribute whose value is injected as a constant string literal. Component.
Read more >
Template Syntax - dart - GUIDE - Angular
We can extend the HTML vocabulary of our templates with components and directives that appear as new elements and attributes. In the following...
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