V
V
vsvladimir2015-05-15 21:20:24
Angular
vsvladimir, 2015-05-15 21:20:24

How to create a service available in the config section and beyond?

It is necessary to make a simple configuration service (several string values), which then users of the module will be able to edit both in the config section, and further at any time. Something like a service of the Value type, but with the ability to use it in the config section as well.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2015-05-15
@vsvladimir


Register a constant service, such as a string, a number, an array, an object or a function, with the $injector. Unlike value it can be injected into a module configuration function (see angular.Module) and it cannot be overridden by an Angular decorator.
angular.module('example').constant('config', {foo: 'bar'});

_
_ _, 2015-05-15
@AMar4enko

angular.module('example', [])
  .provider('MyService', function ($logProvider){
      var var1, var2;
      this.setVar1 = function(value){ var1 = value; }; 
      this.setVar2 = function(value){ var2 = value; }; 
      this.$get = function ($q){
          return new MyService();

          function MyService(){
              this.returnSomething = function (){
                  return $q.when('something ' + var1);
              };
          } 
      };
  });

In the config section, MyServiceProvider will be available with the setVar1, setVar2 methods.
The service will be an instance of MyService with the returnSomething method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question