通过 ngInject 和 ng-annotate 在 AngularJS 中进行双重注入 [英] Double injection in AngularJS via ngInject and ng-annotate

查看:28
本文介绍了通过 ngInject 和 ng-annotate 在 AngularJS 中进行双重注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Gulp 为 AngularJS 应用程序构建我的主要 javascript 文件 (app.js).除了让我烦恼的一件小事之外,一切都运行良好.我正在使用 ng-annotate 自动解析我的 angular js 并添加依赖注入语法.所以我从这个(不使用 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, ...) { 

为此(将被ng-annotate适当修改的代码):

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

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

然而,在重大项目中,如果代码被重用或(喘气)剪切和粘贴,要么单独或作为一套控制器,我喜欢对未来的自己的警告,以及其他开发人员,添加 /* @ngInject */ 注释.像这样:

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

双注入数组存在问题,如下所述:https://github.com/olov/ng-annotate/issues/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*/ 是一种告诉 ng-annotate 您希望它注释某段代码的方法.如果它已经弄清楚了,/*@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.

这篇关于通过 ngInject 和 ng-annotate 在 AngularJS 中进行双重注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆