3

I have a question around the dependency injection based on AngularJS and NodeJS.

There's any difference between $injector from AngularJS and the require module from NodeJS?

Would be nice use require module with a MEAN STACK architecture instead $injector for the Angular app? And for whar propose?

1
  • 3
    Well... They have nothing in common. Node's require() synchronously loads file contents when Angular's $injector manages services. If i understand correctly what you want to do, you may find this video helpful youtube.com/watch?v=4yulGISBF8w Commented Mar 4, 2014 at 19:28

1 Answer 1

4

They're quite different.

Angular's $injector is a classic example of Inversion of contorl. Instead of each module fetching their dependencies, you have an $injector whose job it is to provide dependencies to the modules that are asking for them at run-time. This makes it really to easy switch out the dependencies in tests for example, since nothing is forcing you to pass in the expected dependency -- you could pass in a mock version.

NodeJS's require methods just allow you to require other javascript files and have access to any properties they set on module.exports.

They're not mutually exclusive. You could use browserify (nodejs like require for the front-end) to load the different Angular modules if they are in separate files. It would essentially be equivalent to concatenating them, however. If you wanted to dynamically load the angular modules as needed, you would have to use something like RequireJs.

Conversely, you can use inversion of control in node by passing stuff into a module rather than trying to fetch it from the module. It's actually good practice in many cases.

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

Comments

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.