Глобальная модель в Angular JS?

Рейтинг: 0Ответов: 1Опубликовано: 12.04.2015

Написал сервис в котором задается и возвращается переменная counter:

.factory('CounterNotificationService', function() {

            var counter = {
                feed : 0,
                messages : 0,
                notifications : 0
            };

            return {

                getCounter: function() {
                    return counter;
                },

                setCounterInc: function(field, type) {
                    counter[field] = (type == 'inc') ? counter[field]++ : counter[field]--;
                },

                setCounterValue: function(field, value) {
                    counter[field] = value;
                }
            };
        })

Соответственно в нужных мне контроллерах вызываю этот сервис:

/* test */
        .controller('Tst1Controller', ['$scope','$http','$sce','CounterNotificationService', function($scope, $http, $sce, CounterNotificationService) {
            CounterNotificationService.setCounterValue('messages', 6);
            $scope.tst = CounterNotificationService.getCounter();
        }])

        .controller('Tst2Controller', ['$scope','$http','$sce','CounterNotificationService', function($scope, $http, $sce, CounterNotificationService) {
            //CounterNotificationService.setCounterValue('messages', 4);
            //$scope.tst = CounterNotificationService.getCounter();
        }])

В итоге, я хочу добиться того, чтобы в любом контроллере после обновления значений counter в сервисе - это значение заносилось в $scope.tst и обновлялось на странице, в любом другом контроллере как глобальная переменная.

Ответы

Ответов пока нет.