Best Practices for Using $rootScope.$broadcast in Widgets for Scoped Applications
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
Hi everyone,
I’m working on a ServiceNow scoped application with multiple widgets, and I’m exploring using $rootScope.$broadcast to communicate events between widgets. I’m aware that improper use can lead to performance issues or scope pollution.
I’d like to know the best practices for using $rootScope.$broadcast in ServiceNow widgets, specifically:
How to safely communicate events between widgets
Patterns to avoid scope or memory issues
Recommended alternatives for inter-widget communication in scoped applications
Any advice, examples, or lessons from real-world usage would be very helpful!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @Onlyprashant,
The best practice is to always use uniquely prefixed event names (e.g., my-app.item.updated) and to listen for these events with $scope.$on in your widget's client controller, as this automatically unregisters the listener when the widget is destroyed, preventing memory leaks. For simpler communication, especially between a parent and embedded widgets, passing data through the widget options object is a cleaner method.
However, the most robust and scalable alternative is to create a shared Angular Service (using an Angular Provider record in ServiceNow). This service can be injected into your widget controllers, allowing them to share data and methods directly without broadcasting events, leading to a more structured and maintainable application.
Official reference on Angular Providers:
Hope this helps!
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution and helpful so others can benefit as well.