andNn andNn - 6 months ago 62
AngularJS Question

Angularjs service callback to update scope of controller

A service with a 3rd party library callback function:

mbAppModule.service('aService', function ($http) {{"somedata":0};
var m3rdPartLib="init"; // init
m3rdPartLib.on('timeupdate', function() {;

And a controller

MController: function ($scope, $http, mService) {
$scope.mService= mService;

html page



m3rdPartLib.on() is a 3rd party library callback function which i am using it in a service. I want to show it in the ui as it is getting updated. On callback the value is getting changed, but not getting reflected on ui.

Read some docs and found $rootScope.$apply could be called, but i don't have the reference of $scope / $rootScope in the service.


You can take a dependency on $rootScope and call apply in your service.

mbAppModule.service('aService', ["$http", "$rootScope", function ($http, $rootScope) { = {
        "somedata": 0
    var m3rdPartLib = "init"; // init    
    m3rdPartLib.on('timeupdate', function () {
   = 1;