ngInject 在下面这段代码中做了什么? [英] What does ngInject do in the following piece of code?

查看:27
本文介绍了ngInject 在下面这段代码中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS 控制器代码:

AngularJS controller code:

function AuthConfig($stateProvider, $httpProvider) {
  'ngInject';

  // Define the routes
  $stateProvider

  .state('app.login', {
    url: '/login',
    templateUrl: 'auth/auth.html',
    title: 'Sign in'
  })

  .state('app.register', {
    url: '/register',
    templateUrl: 'auth/auth.html',
    title: 'Sign up'
  });

};

export default AuthConfig;

我无法弄清楚 ngInject 的用途是什么.有人可以帮我吗?

I am not able to figure out what is the use of ngInject. Could someone please help me?

推荐答案

'ngInject'; 本身什么都不做,它只是一个字符串文字.一个叫做 ng-annotate 的工具使用它作为一个标志:如果一个函数以 'ngInject'; 开头,它将被 ng-annotate 处理.

'ngInject'; does nothing by itself, it's just a string literal. A tool called ng-annotate uses it as a flag : if a function starts with 'ngInject';, it will be processed by ng-annotate.

基本上,ng-annotate 会转换

Basically, ng-annotate will transform

angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) {
    "ngInject";
    ...
});

angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", function($scope, $timeout) {
    "ngInject";
    ...
}]);

为了使代码压缩安全.

如果您不使用 ng-annotate,则可以放心地忽略或删除该表达式. 不过要小心,如果它确实使用了 ng-annotate,您可能会中断项目的构建过程.有关 ng-annotate 及其作用的更多信息,请参阅 https://github.com/olov/ng-注释

If you're not using ng-annotate, you can safely ignore or remove the expression. Be careful though, you might break the build process of the project if it does use ng-annotate. For more information about ng-annotate and what it does, see https://github.com/olov/ng-annotate

这篇关于ngInject 在下面这段代码中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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