Jess Jordan Jess Jordan - 23 days ago 7
AngularJS Question

$scope.on in angularjs and binding it with an event

$scope.$on('properties:update', function (event, properties) {
$scope.properties = properties;
console.log('fired');
});


What does the above code does? I did not see any
update
method elsewhere in the controller and in the view. Above event only trigger once upon entering the page. How to trigger it programmatically?

Answer

This method is used for communicating between controllers. Like two controllers in one application can communicate with each other through broadcast and on handlers. See the below example.

The event broadcaster a different controller

$rootScope.$broadcast('showMenu', $scope.ticket_items);

The event reciever is as follows

$scope.$on("showMenu",function(event,ticket_items){
    $scope.ticketVisible = false;
    $scope.ticket_items = ticket_items;
});