通过ngInject和NG-注释双注射AngularJS注释、ngInject、NG、AngularJS

2023-09-13 05:03:26 作者:我已帅出翔

我使用咕嘟咕嘟搭建一个AngularJS应用我主要的JavaScript文件(app.js)。一切正常,除了正在困扰着我一个小小的事情。我使用的NG-注释自动分析我的角度js和添加依赖注入语法。所以我从这个去(手动进样不使用NG-注释):

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, ...) { 

要这样(code,将通过适当的NG-注释进行修改):

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

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

然而,在重大项目,如code重新使用或(喘气)剪切和粘贴,无论是单独或作为套件控制器,我喜欢我的未来的自我警告,并其他开发者的,添加 / * * @ngInject / 注释。像这样的:

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

有与双色注塑阵列一个问题,因为这里要注意: https://开头的github .COM / olov / NG-注释/问题/ 28 。不过,这似乎并不适用于相同的情况下,我在想,如果有一个与双色注塑,我需要是死一般害怕的一大问题,我不能找到其他很多在线上的后果。

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.

推荐答案

没有必要害怕! / * @ * ngInject / 是一种方式来告诉你想让它诠释某一段code的NG-注释。如果它已经想通了这一点, / * @ * ngInject / 不让它做任何额外的东西,是完全无害的。

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.