- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 06:26 AM
Hi ,
Please explain the $scope.on and $scope.destory in servicenow portal
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 08:53 AM
@kali
My apologies I misread your message I thought you were asking for a simple code for $scope.on and destroy.
I added a $scope.broadcast to the previous code to show how it works.
$scope.broadcastCustomEvent = function() {
var eventData = { message: 'This is a custom event!' };
$scope.$broadcast('custom.event', eventData);
console.log('Custom event broadcasted with data:', eventData);
};
// Listen for a custom event
$scope.$on('custom.event', function(event, data) {
console.log('Custom event received with data:', data);
// Handle the event
});
// Clean up when the scope is destroyed
$scope.$on('$destroy', function() {
console.log('Widget scope is being destroyed, cleaning up...');
// Perform cleanup actions
});
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 07:08 AM
Hi @kali ,
This article will explain you all you need: https://www.servicenow.com/community/now-platform-blog/best-practices-for-avoiding-memory-leaks-in-s...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 07:20 AM
Hi @Community Alums ,
Can you explain broadcast using simple code .
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 07:27 AM - edited 07-10-2024 07:31 AM
Hey @kali ,
Heres a simple code
// Listen for a custom event
$scope.$on('custom.event', function(event, data) {
console.log('Custom event received with data:', data);
// Handle the event
});
// Clean up when the scope is destroyed
$scope.$on('$destroy', function() {
console.log('Widget scope is being destroyed, cleaning up...');
// Perform cleanup actions
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2024 08:08 AM
Hi @Dan Essigmann ,
So the custom event is created using broadcast or emit?