Skip to content

Instantly share code, notes, and snippets.

@MiguelLattuada
Last active November 27, 2015 17:04
Show Gist options
  • Save MiguelLattuada/a3e6f0f79310fc6e1c06 to your computer and use it in GitHub Desktop.
Save MiguelLattuada/a3e6f0f79310fc6e1c06 to your computer and use it in GitHub Desktop.
Approach for AngularJS app settings
angular.app('testing', [])
.constant(‘baseSettings’, {})
.value('appSettings', {})
.controller('MainCtrl', function($scope, appSettings){
$scope.$watch(function(){ return appSettings; }, function(old, _new){
//your code here…
});
})
.controller('AnotherCtrl', function($scope , SettingsHelper){
SettingsHelper.setSetting('enableToPerform', true);
})
.factory('SettingsHelper', function(baseSettings, appSettings){
function getSetting(setting){ return appSettings[setting]; }
function setSetting(setting, value){ appSettings[setting] = value; }
function resetSettings(){ appSettings = baseSettings; }
return {
getSetting: getSetting,
setSetting: setSetting,
resetSettings: resetSettings
};
})
.service('SomeService', function(SettingHelper){
this.someMethod = function() {
if(SettingHelper.getSetting('enableToPerform')){
//do it!
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment