I've got a service for my websocket connection and this service returns in a function the actual state whether the client is actual connected or not. I've simplified my code and these snippets are a small example of my problem:
myApp.factory('Websocket', function() {
//the status boolean whether connected or not
var bool = true;
//small mock for the status change
setInterval(function() {
bool = !bool;
}, 1000);
//i've made the value of the status variable accessable in a function or the variable itself
return {
status: function() {
return bool;
},
var: bool
}
});
Now i tried to apply the status value to my scope in the controller:
myApp.controller('Ctrl', function(Websocket, $scope) {
//method 1
$scope.status = Websocket.status();
//method 2
$scope.$watch(function() {
return Websocket.status();
}, function(newVal) {
$scope.status = newVal;
});
});
...but both methods don't work and don't update the $scope variable for this html:
<div ng-app="myApp">
<div ng-controller="Ctrl">
method1: {{status}}
</div>
</div>
This is a codepen: http://codepen.io/anon/pen/bVjaBa?editors=101
Thanks for your help!