I am trying to find a way to create a service that can sit at my highest level scope and hold variables such as the user state and whatever else.
Right now I am trying to create a simple example in which I bind the numbers together. However the UI layer never updates unless I force my angular to go get the value again.
What happens if you remove the line that says getNumber() you see that the number on the html never updates. I tried calling $apply and that give me an error that one is already in effect.
Code:
$scope.update = function () {
SampleService.update();
//Forcing the number to be updated by asking the service to return it
$scope.number = SampleService.getNumber();
};
//Why doesn't this force $scope.number to change as SampleServce.number changes?
$scope.number = SampleService.number;
Service:
(function () {
'use strict';
angular
.module('Services')
.service('SampleService', SampleService);
SampleService.$inject = ['$http'];
function SampleService($http) {
var Service = {
number: 0
};
Service.update = function ()
{
Service.number += 1;
};
Service.getNumber = function ()
{
return Service.number;
};
return Service;
}
})();