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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -