0

I created two directives, lets say A and B:

   <A>
      <B />
   </A>

A is a transclude. And A's and B's scopes are inherited from their ng-controllers.

I want to connect A and B using events. According to they both use ng-controller scope i have to write something like this: in A's controller

$scope.$parent.$broadcast(..)

and in B's controller:

$scope.$on(...)

It works fine.

But what if i dont want to use ng-controllers? Like im developing independed complex directive which contains sub-directs (like A contains B). Is it possible to say angular to inherit B's scope from A's scope? So i would be able to do it like: in A's:

$scope.$broadcast(..)

and in B's

$scope.$on(..)
3
  • Question is too confusing kindly set up a plunker or fiddle demo to illustrate more Commented Jul 3, 2013 at 11:13
  • I am not sure but if you don't create a scope property with object hash, you will inherit the parent directive scope. Just try. Commented Jul 3, 2013 at 12:05
  • How i understand: If you create object hash for scope then your scope is isolated (and it's not inherited from controller). If you set «true» to scope then scope is inherited from controller's one. Commented Jul 4, 2013 at 9:30

1 Answer 1

3

You can communicate between two or more controllers/directives using a service with notification semantics. There is a good example of that here.

The way you would use it is to take a dependency in your directives on pubsub and then use the publish/subscribe methods to send/receive messages.

I've wacked together a demo here

---- UPDATE ----

I had a bit more of a play with this and found that the two directives actually share the scope so you could just update the scopes value directly. You can also use broadcast. I put together another demo of the broadcast method here.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, its clear, it just seemed to be a litte bit more beauty to have ability to inherit scope not from ng-controller but from parent directive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.