A
A
Alexey2016-09-19 10:38:50
Angular
Alexey, 2016-09-19 10:38:50

Service value not updating in AngularJS directive?

I am using Angular version 1.
There is a directive:

.directive('lives', ['freelive', 'liveNum', '$timeout', function(freelive, liveNum, $interval) {
        return {
            restrict: 'AE',
            replace: true,
            freelive: freelive,
            templateUrl: 'templates/directives/lives.html',
            link: function(scope, elem, attrs) {
                scope.liveValue = liveNum.total;
                
                $interval(function() {
                    liveNum.total = liveNum.total - 1;
                    console.info('UPDATED!');
                }, 3000);
            }
        };
    }]);

This directive has scope.liveValue which contains the value from the service:
angular
        .module('starter')
        .value('liveNum', {
            minimum: 0,
            maximum: 20,
            oneTouch: 1,
            total: 20
        });

When changing the value in the directive liveNum.total = liveNum.total - 1; value in template:
<div class="live-value">{{liveValue}}</div>
does not change.
Why? Do I need to use watchers for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sasha, 2016-09-19
@madmages

scope.liveValue = liveNum.total;
                
                $interval(function() {
                    scope.liveValue = scope.liveValue - 1;
                    console.info('UPDATED!');
                }, 3000);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question