I have this code structure in Angular:
app.controller(AlphaCtrl, function(interstellarService){
$scope.interstellarService = {
routeToEarth: interstellarService.get('routeToEarth'),
}
})
app.controller(CentauriCtrl, function(interstellarService){
$scope.interstellarService = {
routeToEarth: interstellarService.get('routeToEarth'),
}
})
The Interstellar Service stores in the Browser storage:
appServices.factory('interstellarService', function ($rootScope, $localStorage){
return {
set: function(entidy, value) {
$localStorage[entidy] = value;
},
get: function(entidy) {
if ($localStorage[entidy] !== undefined) {
return $localStorage[entidy];
} else return false;
}
}
});
Now when I change in the AlphaCtrl the routeToEarth property via a setter method I expect the CentauriCtrl to update accordingly, because the data is bound at each controller to the service, as in:
AlphaCtrl <-- data --> interstellarService <-- data --> CentauriCtrl
This is what does not work: I want to use and share the value stored in routeToEarth in my Centauri-HTML, e.g.
<li>
<a>
{{interstellarService.routeToearth}} && 'Timetravel' || 'Wormhole' }}
</a>
</li>
What am I missing here?
app.controllerandappServices.factory. So are ur factory and controller in different module? If so, did you inject ur sevice properly?