Create factory to refresh badge in all views

I would check if in my app there are new msg, so I would create a factory or service to make every 5 sec an $http request and update a badge in all view of the app!
Is it possible?
Any ideas??

I’m trying this, but dont refresh!

var timer = $timeout(timeoutFunction, 5000);
   timer.then(setNew(), handleError);
  function timeoutFunction() {
  function handleError() {
    console.log('Timer rejected');

  function newMessageReceived(scope, callback) {
    scope.$on('newMessageReceived', function() {

  function getNew() {
    return $localstorage && $localstorage.message || undefined;

  function setNew() {
    var user_id = $localstorage.get('user_id');
    return $''+user_id)
        $localstorage.message = data;
      .error(function(){alert("Errore di comunicazione!")})

  return {
    newMessageReceived: newMessageReceived



You can create intervals or clear intervals.

I would create a service, where you can get your messages and in a controller i would create a interval which calls this method in a specific time.

If you have authorization you can clear the interval on logout.

I have a basecontroller for all my “loggedin”-views -> in that controller i create all background intervals.
And i provide two events where other views can trigger interval-cleaning or creation.

but with interval I could assign value for badge in all view, or only in view where the user is when interval find a new msg?

If you have that badge in all of your view, you should try to find a base-layout of your views.

For example you have in every view a headerbar/navbar with that badge -> you could create that layout with navbar in a base-template -> this is connected with the base controller.
This base template has an own ion-nav-view where the other views can hang in.

In this case it is enough to handle the new-message stuff in that basecontroller.

If you can not work with a base layout -> if the base-controller updates the value -> trigger an event with $scope.$broadcast to inform your views about it.

I change my script so:

.service('messageService', function($rootScope, $http, $localstorage, $timeout){
		return {
		getNew: function(callback){
			var user_id = $localstorage.get('user_id');
    		var timer;
			function myLoop(){
				timer = $timeout(function(){console.log( "Timeout executed")},5000);
					.error(function(){alert("Errore di comunicazione!")});
					function(){console.log("Timer rejected!")}

and the controller is called by the sidemenu:

messageService.getNew(function(data) {
		$scope.counter = data
		} else {
		$scope.counter = {}

Could I use this script to assign to all $scope.counter (the badge) the value?
I think is possible with $scope.$broadcast…but how?

if you set your counter -> do not set it on the local sidemenu scope -> do it on the $rootScope

$rootScope.myNewMessageCounter = data;

try to use a unique key for your counter (because there could be another variable in another scope, that is called “counter” ;)).
Now if you use {{myNewMessageCounter}} in any template there should be your value (if it is not overwritten by another controller).

In your controllers you can use $rootScope.myNewMessageCounter.

If you do not want to put this value on the $rootScope you can trigger a custom event:

$rootScope.$broadcast('newMessage', data);

and in another controller
$scope.$on(‘newMessage’, function (event, data) {
console.log(data); // it should be your message data