Based on the idea that services are singletons ( All Services are singletons ), if I create a simple service with "local" variables and modify this in a controller, when accessing this value in another controller, it should keep the value, right?
Example:
angular.module("app", []);
angular.module("app").service("service", function() {
var _count = 0;
this.incrementCount = function() {
_count ++;
console.log(_count);
};
});
angular.module("app").controller("ctrl1", function($scope, service) {
$scope.increment = function() {
service.incrementCount();
}
});
angular.module("app").controller("ctrl2", function($scope, service) {
$scope.increment = function() {
service.incrementCount();
}
});
<!DOCTYPE html>
<html ng-app="app">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script></head><body><divng-controller="ctrl1">
<button ng-click="increment()" value="ctrl1" >Ctrl1 </button>
</div>
<div ng-controller="ctrl2">
<button ng-click="increment()" > Ctrl2 </button>
</div>
</body>
</html>
In this example, if you analyze the console, it increments the correct value regardless of the controller, but in my case, it happens that I only have one controller on the page and when I go to the next page (without holding the template) , this value resets to 0.
The way I make the system go to another page is by using the window object: window.location.href = "views/player.html"
I do this because my application runs in FILE protocol and I could not at least do it any other way, which prevents me from using templates (at least I could not use them).
Is this really the expected behavior?