The problem is with you parent/child assumption:
- Parent is only a parent element of Child but not a parent scope.
- Actually they are sibling scopes, both children of the $rootScope ($id: 002) in this case.
Why??
- Due to this commit,
Isolate scope is now available only to the isolate directive that requested it and its template
.
- It means that parent directive contents (which included the child directive) are still linked to the outer scope.
- So the child directive creates isolated scope which is a child of the outer scope.
- Neither $emit nor $broadcast would work with sibling scopes.
Solution:
here is a plunker: http://plnkr.co/edit/EfKJthkQLitWnF2srykM?p=preview
You can create the child directive as a template of the parent directive, because template directives do get the directive's scope as mentioned above.
.directive('parent', function ($timeout) {
return {
template:'<child name="Jimmy"></child>',
do you really need the event bus?
Another solution would be to create a controller on the parent directive and to require it in child directives.
.directive('parent', function ($timeout) {
return {
controller: function(){
this.hungry = function(message){
// something
}
}
child directive:
.directive('child', function ($timeout) {
return {
require: "^parent",
link: function (scope, element, attrs, parentCtrl) {
parentCtrl.hungry("I'm hungry!")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…