Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

transcludeControllers of $compile return function not work #13793

Closed
szaccaria opened this issue Jan 18, 2016 · 5 comments
Closed

transcludeControllers of $compile return function not work #13793

szaccaria opened this issue Jan 18, 2016 · 5 comments

Comments

@szaccaria
Copy link

Perhaps I don't understand the document at the end of https://docs.angularjs.org/api/ng/service/$compile, but when I try to pass the controller to directive seem not work.

Es: https://jsfiddle.net/qq4gqn6t/5/ don't work (the parent controller not pass to child directive)

Es: https://jsfiddle.net/qq4gqn6t/6/ work but not use the "transcludeControllers" options, like is write in document.

@Narretz
Copy link
Contributor

Narretz commented Jan 19, 2016

Hi,
it seems like the docs are wrong and transcludeControllers actually expects objects like this:

transcludeController: {
  dad: {
    instance: vm
  }
}

I can't tell you why there's another level of indirection - maybe it's left over from some earlier code.

@Narretz Narretz added this to the Backlog milestone Jan 19, 2016
@szaccaria
Copy link
Author

Thanks Narretz!
Now work like a charm! See here https://jsfiddle.net/qq4gqn6t/8/
Perhaps could be correct the documentation, you think?

@szaccaria
Copy link
Author

A little more clear, perhaps is better write in the documents that the controller of parent is reachable only from the child directive:

Look here:

https://jsfiddle.net/qq4gqn6t/8/ in particular at row 30 "require: ['?^csDad', 'csSon']", I put "?^cdDad" (?^ - Attempt to locate the required controller by searching the element and its parents or pass null to the link fn if not found.)
But if you put "require: ['?^^csDad', 'csSon']", (?^^ - Attempt to locate the required controller by searching the element's parents, or pass null to the link fn if not found.) not work.

I think that the issue is that the controller, trought the "transludeControllers", is put to the child directive and not to parent (from where it come)

I hope I have been clear

Thanks again Narretz

@Narretz
Copy link
Contributor

Narretz commented Jan 19, 2016

I thought it was clear that the transcludeControllers would be applied to the compileNode (i.e. the top level node in your compile fn). That basically means the controller instances are put as data into the compileNode. So any directives on the compileNode and its children that require controllers can access the data.
The reason why ^^ doesn't work is simple: ^^ specifies that the directive should look for this controller only on the parent element, not on the element itself. So when you compile this node with the son directive, it'll look for the dad controller on the parent, but since there is not parent, it'll throw.

@szaccaria
Copy link
Author

You are right!
I re-read the documents, and now it been seem more clear, I've wrote the precedent post only for ask your opinion if it necessary to add the note at the documentation for make more clear it... but now for me it's all clear (at least for this argument :) . Thanks Narretz... and sorry for my bad english :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants