Skip to content

Instantly share code, notes, and snippets.

@joladev
Last active April 18, 2016 13:28
Show Gist options
  • Save joladev/6989249 to your computer and use it in GitHub Desktop.
Save joladev/6989249 to your computer and use it in GitHub Desktop.
Simple AngularJS State Service for sharing state between controllers.
angular.module('services', [])
.factory('State', function ($rootScope) {
'use strict';
var state;
var broadcast = function (state) {
$rootScope.$broadcast('State.Update', state);
};
var update = function (newState) {
state = newState;
broadcast(state);
};
var onUpdate = function ($scope, callback) {
$scope.$on('State.Update', function (newState) {
callback(newState);
});
};
return {
update: update,
state: state,
listen: onUpdate
};
});
@sgruhier
Copy link

listen: listen but you don't have any listen method/objectin your factory

@marpstar
Copy link

Should probably be listen: onUpdate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment