0

chrome.runtime.getBackgroundPage(function callback) is the best way to access a global variable in the options page defined from background page. For example, Settings here.

var extension = chrome.extension.getBackgroundPage();
var Settings = extension.Settings;

That any change made in options page to Settings can also be accessed by background page.

But I use the angularjs in the options page, I have to bind the data to the scope. In any angularjs controller:

angular.extend($scope, Settings);

I should also watch specific property and update the Settings:

$scope.$watch('quickSwitchEnabled', function() {
  Settings.quickSwitchEnabled = this.last;
});

Only in that way, I can update the Settings when I changed some model.

But angular can't follow any changes made to Settings object by background page. Once I update the Settings in the background page, there would be no esay way to notify angularjs about that. It seems I have to manually watch the Settings and notify the angular in every controller I use the Settings.

Are there any better way to do this?

Community
  • 1
  • 1
erickg
  • 975
  • 7
  • 18
  • If I understand what you're asking; you can put the "Settings" object into an AngularJS Factory or controller that also has methods for updating the object as appropriate. Route all changes to the settings via the angular factory instance. – JeffryHouser Mar 17 '14 at 18:03
  • @Reboog711 I want to make a binding with global variable `Settings` and $scope to reduce watching scope change all over the place since `Settings` only contains data. For any incoming change made to `Settings` outside the angularjs, I also have to notify angularjs about it. – erickg Mar 18 '14 at 06:53

0 Answers0