I am currently using a service that has a variable which needs to be updated by the service itself. However, I am facing an issue where I cannot access the variable in anonymous or delegated functions.
(function() {
'use strict';
angular
.module('yoTest')
.service('mainService', mainService);
/** @ngInject */
function mainService($timeout) {
this.counter = 1;
this.updateCounter = function updateCounter() {
this.counter++;
$timeout(updateCounter, 500);
}
this.updateCounter();
}
})();
Whenever I try to reload the "updateCounter" using $timeout
, I encounter an error. Why is this happening?
Is there a way to access it using timeout and delegate/callback methods?