So, I've got a counter ($scope.counter
) that I increment on ng-click
. The problem I'm having is that I want to increment it from two different controllers, keeping the counter value in sync.
controllerA.js
.controller('controllerA', controllerA);
function controllerA($scope) {
$scope.counter = 0;
function incrementCounter() {
$scope.counter = $scope.counter + 1;
}
...
controllerB.js
.controller('controllerB', controllerB);
function controllerB($scope) {
$scope.counter = 0;
function incrementCounter() {
$scope.counter = $scope.counter + 1;
}
...
When I call the incrementCounter()
on 'controllerA'
it updates the counter on 'controllerA'
, but not on 'controllerB'
and vice versa.
Is there a proper way to keep these in sync, no matter which controller I call the incrementCounter()
function from?
Any help is appreciated. Thanks in advance!
$rootScope
, which is not good practice rather use service as suggested be @jbrown