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

Bug? ContentChild not working with nested directives. #10121

Closed
vagarenko opened this issue Jul 17, 2016 · 4 comments
Closed

Bug? ContentChild not working with nested directives. #10121

vagarenko opened this issue Jul 17, 2016 · 4 comments

Comments

@vagarenko
Copy link

I'm submitting a ... (check one with "x")

[x] bug report
[ ] 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
Consider this code:

import {Component, Directive, ContentChild} from '@angular/core'

@Directive({
  selector: '[target]'
})
export class Target {}

@Directive({
  selector: '[findTarget]'
})
export class FindTarget {
  @ContentChild(Target) target: Target;

  ngAfterContentInit() {
    console.log('Content', this.target)
  }
}

@Component({
  selector: 'comp',
  template: `<div target>Target</div>`,
  directives: [Target]
})
export class Comp {}

@Component({
  selector: 'my-app',
  template: `<comp findTarget></comp>`,
  directives: [Comp, FindTarget]
})
export class App {}

plunker: http://plnkr.co/edit/mZdFvTVTFGO01DFnITZc?p=preview
I have 2 directives target and findTarget and I want to find if element with findTagret directive has a descendant with target directive. This works when both directives are in the same template:

<div findTarget><div target></div></div>

but doesn't work if they are in different components.
Using ViewChild instead of ContentChild doesn't work in both cases.
Using Query doesn't work either.
Is this expected behaviour? If so, what is the proper way to do this?

@DzmitryShylovich
Copy link
Contributor

According to the docs ngAfterContentInit is component only hook.

@vagarenko
Copy link
Author

vagarenko commented Jul 17, 2016

@DzmitryShylovich
It is being called and I get Content undefined in console.
Also other docs https://angular.io/docs/ts/latest/api/core/index/ContentChild-var.html has:

@Directive({
  selector: 'someDir'
})
class SomeDir {
  @ContentChild(ChildDirective) contentChild;
  @ContentChild('container_ref') containerChild
  ngAfterContentInit() {
    // contentChild is set
    // containerChild is set
  }
}

@vicb
Copy link
Contributor

vicb commented Jul 17, 2016

It is the expected behavior, you can only query direct children.

The rationale being that you might not know about inner component implementation and should not be affected by them.

@vicb vicb closed this as completed Jul 17, 2016
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants