Skip to content

Instantly share code, notes, and snippets.

@advincze
Last active December 17, 2015 19:59
Show Gist options
  • Save advincze/5664807 to your computer and use it in GitHub Desktop.
Save advincze/5664807 to your computer and use it in GitHub Desktop.
angularjs share data state between service and controller
var myApp = angular.module('myApp', []);
myApp.factory('srv', function ($rootScope) {
srv = {
arr: []
};
var val = 1
setInterval(function () {
$rootScope.$apply(function () {
srv.arr.push(val++);
if (srv.arr.length > 3) {
srv.arr.shift()
}
});
}, 500);
return srv
});
function MyCtrl($scope, srv) {
$scope.arr = srv.arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment