In my application I want to have a self-updating data service, which is injected to the controllers:
app.factory("StatsService", function() {
var service;
service = {
data: 0,
init: function() {
var self = this;
setInterval(function() {
self.data = Math.floor((Math.random() * 100) + 1);
}, 1000);
}
};
service.init();
return service;
});
Here's an example controller:
app.controller("SourcesController", ['$scope', 'StatsService', function($scope, StatsService) {
$scope.data = StatsService.data;
$scope.$watch('data', function(val) {
// this does not work either
});
}
How I can make controller reflect service changes? I don't know whether I did something wrong with service or with controller, it just doesn't work.