You can do that using angular.factory()
or angular.service(); or $localStorage and or $sessionStorage.
First to make a global call instead of calling inside a controller:
Example:
angular.factory('updateVal', function(){
var data;
return {
getPlayers: function(){
return $http.get('/players').success(function(results){
data = results;
return data;
});
},
setPlayers: function(val){
if(val){
data = val;
return data;
}
else {
return data;
}
}
}
});
In your controller:
First controller
angular.module('app').controller('nav', function($scope,updateVal){
$scope.numOfPlayers = updateVal.getPlayers();
$scope.$watch(function(){
return updateVal.setPlayers().length > 0;
}, function(){
$scope.numOfPlayers = updateVal.setPlayers();
})
})
Second controller:
angular.module('app').controller('mainController', function($scope,$http,updateVal) {
// this function update the players updateVal.getPlayers();
$http.post(.....).then(function(data) {
//update the numOfPlayers so the nav is updated.
// after posting call this function:
updateVal.setPlayers(data);
});
});
Using angular.service :
This can be done :
angular.service('updatePlayers', function(){
var updatedPlayers;
this.setPlayers = function(args){
updatedPlayers = args;
}
this.getPlayers = function(){
return updatedPlayers;
}
})
In your First controller:
angular.module('app').controller('nav', function($scope,$http,updatePlayers) {
$http.get('/players').then(function(data) {
updatePlayers.setPlayers(data.players.length);
$scope.numOfPlayers = data.players.length;
});
});
In the second controller:
angular.module('app').controller('mainController', function($scope,$http,updatePlayers) {
$http.post(.....).then(function(data) {
//update the numOfPlayers so the nav is updated.
updatePlayers.getPlayers();
});
});
EDITED to fix typo