Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double injection in AngularJS via ngInject and ng-annotate

I'm using Gulp to build my main javascript file (app.js) for an AngularJS application. Everything is working fine except for one small thing that is bothering me. I am using ng-annotate to automatically parse my angular js and add dependency injection syntax. So I went from this (manual injection without using ng-annotate):

angular.module('base.controllers')
        .controller('RandomeCtrl',
                ['$scope', '$routeParams', ...,
                    function($scope, $routeParams, ...) { 

To this (code that will be modified appropriately by ng-annotate):

angular.module('base.controllers')
        .controller('RandomeCtrl', 
                    function($scope, $routeParams, ...) {

However, in major projects, if the code is re-used or (gasp) cut-and-pasted, either alone or as a suite of controllers, I like the warning for my future self, and for other devs, of adding the /* @ngInject */ annotation. Like this:

angular.module('base.controllers')
        .controller('RandomeCtrl', 
        /*@ngInject*/
                function($scope, $routeParams, ...) {

There was a problem with double injection arrays, as noted here: https://github.com/olov/ng-annotate/issues/28. However, this doesn't seem to apply to the same scenario, and I was wondering if there was a major issue with double injection that I need to be deathly afraid of, and I can't find much else online on the consequences.

like image 229
oberger Avatar asked Dec 25 '22 02:12

oberger


1 Answers

No need to be afraid! /*@ngInject*/ is a way to tell ng-annotate that you want it to annotate a certain piece of code. If it had already figured that out, /*@ngInject*/ doesn't make it do anything extra and is entirely harmless.

like image 139
olov Avatar answered Jan 07 '23 11:01

olov