angularjs - Different ways to inject dependencies -


when writing ex. directives can reference/inject dependencies both in directive factory function like:

app.direcive('directivename', function ($http) { ... }; 

or in controller

app.directive('directivename, function () {     ...     controller: function ($http),     ... }; 

also today i've read post made me think what's difference between 2 approaches?

there many ways. none right or wrong. style. use 1 way project, clarity , consistency.

this suggest:

yourcontroller.$inject = ['http']; function yourcontroller($http) {}  function yourdirectivename() {     return {         controller: yourcontroller,         controlleras: 'vm',         bindtocontroller: true     } }  app.directive('yourdirectivename', yourdirectivename); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -