Skip to content

Instantly share code, notes, and snippets.

@plugn
Forked from joladev/state.js
Created April 18, 2016 13:28
Show Gist options
  • Select an option

  • Save plugn/eca91b52349a060b099389c22f72b3ad to your computer and use it in GitHub Desktop.

Select an option

Save plugn/eca91b52349a060b099389c22f72b3ad 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
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment