I have two controllers and a factory. I am injecting the factory and a controller into a main controller, here are the code snippets below:
Factory:
app.factory('TallyFactory', function(){ return { correct: 0, incorrect: 0 }; }); TallyController that accepts the TallyFactory:
app.controller('TallyController', function($scope, TallyFactory){ $scope.$watch('scores', function(newValue, oldValue){ console.info('changed!'); console.log('Old: ' + JSON.stringify(oldValue)); console.log('New: ' + JSON.stringify(newValue)); }); $scope.scores = TallyFactory; }); MainController which accepts the TallyFactory and changes the value of the TallyFactory Object:
app.controller('MainController', function ($scope,TallyFactory) { $scope.addTally = function(){ TallyFactory.correct++; } }); Notice in my TallyController (second snippet), I added a watcher to the $scope, listening for when "scores" changes.
When I run the "addTally" function from my MainController with an ng-click, it changes the value of my Factory Object (TallyFactory). Since there was a change, and I specifically created a watcher on my TallyController ($scope.scores), shouldn't the digest cycle be triggered and my console update with the new value of my TallyFactory?
Also, my TallyController is NOT nested in my MainController, they are separate from each other (not 100% sure if that matters).
I'm not sure why the Digest Loop isn't triggered since the TallyFactory changed values.


$scope.$watch('scores.correct', ...trueas the 3rd argument on the watch.$scope.$watch('scores', function(){}, true)valueis shorter). The object returned by this factory is the same every time the service is injected. It won't be a singleton otherwise.