Skip to content

Instantly share code, notes, and snippets.

@flrent
Created April 25, 2014 14:41
Show Gist options
  • Save flrent/11291949 to your computer and use it in GitHub Desktop.
Save flrent/11291949 to your computer and use it in GitHub Desktop.
Angular update controller on service changes
angular.module('recorder', [])
.controller('MainCtrl', ['Storage', '$scope', '$timeout', function(Storage, $scope, $timeout) {
$scope.storage = Storage;
$scope.$on('valueChanged', function (evt, selectors) {
$scope.$apply(function() {
$scope.selectors = selectors;
});
});
}])
.service('Storage', function($rootScope) {
this.selectors = [];
var that = this;
chrome.storage.sync.get('selectors', function(value) {
$scope.$apply(function() {
that.selectors = value;
});
});
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (key in changes) {
var storageChange = changes[key];
console.log('Storage key "%s" in namespace "%s" changed. ' +
'Old value was "%s", new value is "%s".',
key,
namespace,
storageChange.oldValue,
storageChange.newValue);
if(key=='selectors') {
that.selectors = storageChange.newValue;
}
}
$rootScope.$broadcast('valueChanged', that.selectors);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment