Servicenow portal

kali
Tera Contributor

Hi ,

Please explain the $scope.on and $scope.destory in servicenow portal

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

@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
        });
    }

 

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi @Community Alums ,

Can you explain broadcast using simple code .

Thanks in advance

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
        });
    }

 

 

Hi @Dan Essigmann ,

So the custom event is created using broadcast or emit?