3

I want to have a set of reusable widgets which can be used anywhere in the application. For ex. I want to have a settings menu and the menu items defined as nested custom tags. I want to be able to send events from the menu items or menu to the parent context (either the controller in parent directive or whatever).

But I figured out some strange things with scope (maybe I misunderstood something):

<panel title="clock">
    <clock timezone="MST"></clock>
    <clock timezone="MST"></clock>
    <clock timezone="MST"></clock>
</panel>

The following example doesn't fire the event to the parent directive controller when click on the items: http://jsfiddle.net/9VTfR/2/.

But the following works fine: http://jsfiddle.net/9VTfR/3/.

Also I got some strange JS error in the console for both options.

So is it possible to catch the events at any level as the parent directive should be the parent scope as well (not only the parent controller), no? Any help would be appreciated.

1 Answer 1

6

The error you're seeing the console is because you've got Angular included twice in your fiddle (once in External Resources; once in Frameworks & Extensions).

The reason your $scope.$on('clickObject') isn't firing is because both of your directives are siblings and $emit only notifies upwards

From the docs:

Dispatches an event name upwards through the scope hierarchy notifying the registered listeners.

You can use $scope.$parent (like this) but that makes some assumptions about how your directives will be structured. You can also use $rootScope (like this), there's a rare chance you'll run into conflicts if you don't use unique-enough event names.

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

2 Comments

Ok. I understand. Thanks. I didn't know about this: In a typical setup the widget creates an isolate scope, but the transclusion is not a child, but a sibling of the isolate scope. And I thought that if I use nested directives they have a parent-child relationship.
Ya, it doesn't appear transclusion modifies scope. If your <clock> elements were inside of your panel's template I imagine the relationships would be as you'd expect.

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.