I am trying to share data between controllers in angularjs using a factory. I have followed other answers to similar questions but I can only get this to work 1st time round. I have an app with 3 states. 1 of the states is a simple calculator. I want the total in the calculator state to also be displayed in a different state. With the code below the total is always displayed as 0. So i think it works 1st time round but does not update when 'total' is changed. Anyone any suggestions??
html:
<div class="list card">
<div class="item item-divider">Total</div>
<div class="item item-body">
<div ng-model="figure">
<b>{{figure}}</b> calories consumed today
</div>
</div>
</div>
'home' controller:
.controller('homeCtrl', function($scope, total) {
$scope.figure = total.get();
})
calculator controller:
.controller('calcCtrl', function($scope, total) {
$scope.result = 0;
$scope.reset = function() {
$scope.result = 0;
};
$scope.add = function(i) {
if(!i){
}else {
$scope.result = $scope.result + parseInt(i);
total.set($scope.result);
}
};
});
factory
.factory('total', function() {
var val = 0;
return {
get: function() {
return val;
},
set: function(num) {
val = parseInt(num);
}
};
})