I have two controllers like so:
myApp.controller('Ctrl1', ['$scope', '$filter', '$http', 'DataService', function ($scope, $filter, $http, DataService){
DataService.set('HelloWorld', 'TheValue')
alert(DataService.value.TheValue + 'in Ctrl1');
}]);
myApp.controller('Ctrl2', ['$scope', '$filter', '$http', 'DataService', function ($scope, $filter, $http, DataService){
alert(DataService.value.TheValue + ' in Ctrl2');
}]);
And my service is defined like so:
myApp.service("DataService", function () {
var data = {
TheValue: null
};
function set(value, field) {
data[field] = value;
}
return {
set: set,
value: data
};
});
In Ctrl1, the alert shows that the value of TheValue is correctly HelloWorld. But when I navigate to Ctrl2, TheValue becomes null.
How do I get the TheValue value in DataService to persist between controllers?
Edit:
Here is how I navigate between controllers:
My application is a Asp.Net MVC app. Ctrl1 is the AngularJS controller for the Dashboard page, while Ctrl2 is the controller for the next page via a button click and the routing to the new Asp.Net controller.
Dashboard Page snippet:
<body>
<div class="row" ng-controller="Ctrl1">
<div class="col-sm-4">
<ul class="dashboard-list">
<li>
<div class="tile">
<a href="/NextPage" title="The Next Page">
<h2>Schedule New Visit</h2>
</a>
</div>
</li>
</ul>
</div>
</body>
Then, NextPage is just like so:
<body>
<div ng-controller="Ctrl2" class="ng-cloak">
@*Page stuffz here...*@
</div>
</body>