Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

\$rootScope.\$on is not listening to the event broadcasted

chandrashekar3
Giga Contributor

Hi All,

I have braodcasted an event in a widget, but the $on is not listening to the event

below is my code, let me know for any mistakes

Broad cast event

function($scope, $http, spUtil, $rootScope, $sce) {

  /* widget controller */

  var c = this;

this.onClick = function($event){

$rootScope.$broadcast('prepopulateItem', c.data.task_sysid);

};

}

Listening event in SC Catalog Item widget

$rootScope.$on('prepopulateItem', function(event,obj) {

$scope.data.sc_cat_item._fields["IO:8d23d26fdbbab24096fcf9971d961955"].value = 'complaint';

$scope.data.sc_cat_item._fields["IO:44b71623db3eb24096fcf9971d961922"].value = 'Option_case_Ticket';

$scope.data.sc_cat_item._fields["IO:02dde3afdb76364096fcf9971d96199d"].value = obj;

});

7 REPLIES 7

Pedro Silva4
Kilo Guru

prussrobin
Tera Contributor

Hi,



you could try to use a Angular Service. In the 'Angular Providers' module (subitem of Service Portal) create a new record with type 'Service' with the following code in the client script:



function($rootScope){


        var _currentRecord = {};



        this.initialize = function(current){


                  this._currentRecord = current;


                  this._currentParent = current;


        }



        this.setCurrent = function(record){



                  if(this._currentRecord = record){


                            $rootScope.$broadcast('current-changed', record);


                            return true;


                  } else {


                            return false;


                  }


        };



        this.getCurrent = function(){


return this._currentRecord;


};


}




//$rootScope.$on('current-changed', ...});


                                     





I set the name of this service to 'dataShare'



Then in both widgets, add the created service as an Angular Provider (related list for widgets) and in the function definition of the client script, e.g.:



function ($scope, $rootScope, dataShare) {


        //your code here


}



After this you can set the data on the first page using the following function in the client script:



dataShare.setCurrent(object);



Which triggers the 'current-changed' broadcast. So on the second page add the following code in the client script:



$rootScope.$on('current-changed', function(event, object) {


      $scope.data.share = object;


});




or to retrieve the data manually:



$scope.data.share = dataShare.getCurrent();




Hope this is helpfull


Bob Loblaw
Mega Guru

You may have to put a $timeout function around it to prevent it from firing too quickly. $broadcast on a button click (for example) doesn't need a $timeout, since it's needed to fire right when the button is clicked. However, if $broadcast is needed upon widget/page load, it might take a few extra milliseconds for the load to finish and then you want the $broadcast to fire, so that's why you would need a timeout in that situation.

$timeout(function(){
   $rootScope.$broadcast('testWork', 'Hello');
}, 2000);