Double injection in AngularJS via ngInject and ng-annotate

对着背影说爱祢 提交于 2019-12-23 13:03:48

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/26677519/double-injection-in-angularjs-via-nginject-and-ng-annotate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!