本文介绍了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";
    ...
}]);

以使代码的代码安全性高.

in order to make the code minification-safe.

如果您不使用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在以下代码段中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 05:11