0

I have my notification listener in the run function. When a notification is received I need to update a object present in $scope with a parameter present in notification object.

angular.module('app', ['ionic', 'chatsCtrl'])
.run(function($state, $ionicPlatform) {
  window.FirebasePlugin.onNotificationOpen(function(notification) {
    // Need to append this notification.parameter to a scope variable present in a controller  
  }
}
.controller('chatsCtrl', function($scope) {
  // $scope.chats
});

How can I go about doing this? I don't want to use $rootScope object as $scope.chat object will get very heavy.

Thanks

1

1 Answer 1

1

you can't call scope variables/functions inside run block. since you don't want to use rootscope my suggestion is to create a service and assign values to a particular method in that service from the run block. Then get that value from the controller using the same service.

angular.module('app', ['ionic', 'chatsCtrl'])
.run(function($state, $ionicPlatform) {
  window.FirebasePlugin.onNotificationOpen(function(notification) {
     sampleService.setData(notification)
  }
}
.controller('chatsCtrl', function($scope,sampleService) {
   $scope.chats =  sampleService.getData()
});
.factory('sampleService', function() {
   var data;
   return {
     getData : function(){ return data},
     setData: function(param){ data = param},
   }
});
Sign up to request clarification or add additional context in comments.

4 Comments

here $broadcast may be a better way
yeah but try to avoid listeners like broadcast as much as possible. it will reduce the performance and increase the complexity
@sachilaranawaka I implemented what you suggested. But the data was not persisting. So I had to store it in localStorage till I retrieved it in the controller. Or does the factory persist data?
@Pradeep Rajashekar if you are not overriding the get method again data should be persistent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.