4

I have two modules in my angular.js app. In module1 (example name) I have a value defined like

angular.module('module1')
    .value('ID', '')
    .service('someService', function(ID) {
        this.getId = function() {
            return ID;
        }
        this.setId = function(id) {
            ID = id;
        }
    })

I would like to access module1's ID value in module2. I can access module1 from module2 using `

angular.module('module1')

log in console will be`

Object {_invokeQueue: Array[39], _configBlocks: Array[1], _runBlocks: Array[1], requires: Array[8], name: "module1"}

when I try to access ID value or someService using

angular.module('module1').service("someService");

or

angular.module('module1').value("ID");

I am getting strange object looks like

Object {_invokeQueue: Array[40], _configBlocks: Array[1], _runBlocks: Array[1], requires: Array[8], name: "module1"}

Also I can't include module1 in the module2 on initialization, using this style

angular.module('module2', ['module1']);

because I already have module2 included in module1

angular.module('module1', ['module2']);
4
  • Does the same value of ID exist in both modules? Commented Jun 1, 2016 at 8:28
  • @MathewBerg It exist only in module1 Commented Jun 1, 2016 at 8:29
  • Then you should be able to DI it into either if you've included module2 in module1. Commented Jun 1, 2016 at 8:29
  • @MathewBerg Can you explain more detailed, please Commented Jun 1, 2016 at 8:32

1 Answer 1

1

You still need to inject it as a dependency as you would with a controller or service etc.

If you wanted to access it in a controller you could have code similar to this

angular.module('module2').controller('MyExistingCtrl', ['ID', function(ID){
    console.log(ID);
}]);

Or, following John Papa's styleguide:

angular.module('module2').controller('MyExistingCtrl', MyExistingController)

MyExistingController.$inject = ['ID'];

function MyExistingController(ID){
    console.log(ID);
}

Read more on dependency injection.

Also it is explained on the angular documentation for providers. Scroll down the "Value Recipe" section.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Matt, thanks for answer. As I understood your code will create a new controller inside of module1 in the case I need to access 'ID' value in module2's existing controller.
@styopdev Well, that was just example syntax. Just swap the module and controller name to suit your needs. You are just injecting, the same as you inject anything else. I will edit.
It works, thanks for help! I couldn't imagine that solution is so evident and its possible to inject other module's value in controller.
@styopdev no problem, glad to help! As long as the module is included as a dependency, you can inject all it's values, services, constants etc. anywhere. This is the basis of how third party modules work. Include the module -> get access to all services.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.