Skip to content

gilf/ngConfig

Repository files navigation

ngConfig

A simple configuration service for AngularJS 1 apps.

Copyright (C) 2015, Gil Fink gil@sparxys.com

Installation

You can choose your preferred method of installation:

Usage

Include ngConfig.js in your application.

<script src="components/ngConfig/ngConfig.js"></script>

Add the module ngConfig as a dependency to your app module:

var myapp = angular.module('myapp', ['ngConfig']);

configuration service

Use the APIs that the configuration service exposes to use your application configuration. In the API you have four functions:

  • init(): initializes the configuration service. If you are loading the configuration from file, by default the configuration service will look a file called config.json which exists in a config folder -'config/config.json'. Usage example:

    config.init().then(function() {
    
    });
  • isInitialized(): checks whether the configuration object was loaded into memory. Usage example:

    if (!config.isInitialized()) {
      config.init().then(function() {
    
      });
    }
  • getConfigByKey(key): retrieve a configuration according to the supplied key.

    var val = config.getConfigByKey('key');
  • setConfigOfKey(key, value): sets the configuration of the supplied key to new configuration. When you set the key in a scenario of loading the configurations from file, only the in-memory object is updated (the value isn't saved to the configuration file).

    config.setConfigOfKey('key', 'value');

You can configure the configuration file Uri by using the setConfigUri function when you configure your module.

var app = angular.module('app', ['ngConfig']);
app.config(['configProvider', function (configProvider) {
  configProvider.setConfigUri('config/yourFileName.json');
}]);

You can use localStorage to hold your configurations. Use useLocalStorageForConfig with an application prefix string to use localStorage.

var app = angular.module('app', ['ngConfig']);
app.config(['configProvider', function (configProvider) {
  configProvider.useLocalStorageForConfig('myAppName');
}]);

An example of a configuration file:

{
  "config1": 15,
  "config2": false,
  "config3": "config3",
  "config4": [],
  "config5": {}
}

As shown in the Following Post:

http://blogs.microsoft.co.il/gilf/2015/06/22/building-a-simple-angularjs-configuration-service/

License

Released under the terms of the MIT License.

About

A simple configuration service for AngularJS 1 apps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published