Last active
April 18, 2016 13:28
-
-
Save joladev/6989249 to your computer and use it in GitHub Desktop.
Simple AngularJS State Service for sharing state between controllers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
listen: listen but you don't have any listen method/objectin your factory