I was creating a directive to display a chart. I went through the usual templates of creating a module, with a directive. For configuration, I decided to use a Provider. I understood how I could have the application set up my module's configuration through the provider.
However, one use case that I have is that the configuration can change based on the user's preferences at run-time. How do I enable my module to provide a way for the client API to modify the configuration at run-time?
Is it possible to inject the ProviderConfiguration into the Client's Controller as well? Are their any significant drawbacks to this approach?
The current code template looks like this
//Provider
angular.module('foobar', []).provider('foobarConfig', [function() {
var config = {
//config properties here.
}
var configurationProvider = {
//Getters and Setters that work on 'config'.
setProperty(prop, value) {
if(config[prop] !== undefined) {
config[prop] = value;
}
},
getProperty(prop) {
return config[prop];
},
$get: function() {
return this;
}
}
return configurationProvider;
}
//Chart Directive.
angular.module('foobar').directive('foobarChart', ['foobarConfig', function(foobarConfig) {
//Can use the foobarConfig here.
}]);
angular.module('clientApp', [ 'foobar'])
.config(['foobarConfigProvider', function(foobarConfigProvider) {
//Can provide the initial configuration to the module here.
foobarConfigProvider.setProperty("foo", "bar");
}]);
angular.module('clientApp').directive('clientFoo', function() {
//What should be done to change foobarConfig here?
});