Asaf Katz
Asaf Katz

Reputation: 4688

angularjs | dependency injection advantage over requiring modules

what is the advantage of using dependency injection over requiring modules?

/* with dependency injection  */
app.controller('testCtrl', function(dep){
    /* use dep... */
});

/* with require  */
app.controller('testCtrl', function(){
    var dep = require('./dep');
    /* use dep... */
});

Upvotes: 0

Views: 81

Answers (1)

Mior
Mior

Reputation: 831

Those are 2 distinct things.

require is AMD's thing which solves loading of modules so you don't need to include <script> tag in order to load .js file. For that you can use libraries like requireJs

angular dependency injection is angular's thing to load angular modules like $scope, $http etc but those you have in angular and don't need to be loaded with require.

AMD you can use to load another files like another module, service or factory in different file and you want asynchronously load this file when needed you use define() or require() (see requirejs documentation) to load those files. Once you have files loaded with require you need to use angular's DI to to get reference to them in order to use them.

Without require you would need to have files from your service included somewhere in index.html in <script> tag

this is good article about using requirejs in angular.

Hope this will help you clarify stuff.

Upvotes: 1

Related Questions