$注入上angularjs注解 [英] $inject Annotation on angularjs

查看:205
本文介绍了$注入上angularjs注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到 带有$注入控制器上标注的参考,这意味着角种子我可以声明的依赖与控制器。

I've see that Angular Seed comes with $inject annotation on Controllers reference, which means that I can declare the dependencies for the controller with that.

借助 DI文档表明,构造函数必须与当前参数OS控制器同步。

The DI documentation shows that the constructor need to be in sync with current arguments os Controller.

此外,在本文档,这是显示了一种使用此批注,有用的精缩上JS源。像这样的:

Also, in this documentation, it was shows one use of this annotation, usefull on minified JS sources. Like this:

var MyController = function(renamed$scope, renamedGreeter) {
  ...
}
MyController.$inject = ['$scope', 'greeter'];

在这里我找到了一个解释<一个href=\"http://stackoverflow.com/questions/14361692/why-doesnt-appctrl-have-inject-but-myctrl1-does-in-this-angularjs-example\">here.

所以,我的疑问是:


  1. 有这个注解其它用途?

  2. 在非缩小的JS源,它有什么优势或劣势,不使用它?

先谢谢了。

推荐答案

$注射的目的是为了确保你的依赖被正确注射,如果你的code已经过压缩。

The purpose of $inject is to ensure your dependencies are injected properly if your code is minified.

有三种方式来管理AngularJS依赖 - DI在AngularJS

There are three ways to manage dependencies in AngularJS - DI in AngularJS

数组的方式是我的preferred方法,因为它比$注射方法简单。

Array Notation is my preferred approach as it is simpler than the $inject approach.

要回答您的具体问题:


  1. 不,我不知道有其他的用途$注入。

  2. 如果您不使用链接的文章中提到的前两种方法之一,如果有人minifies源的code将无法正常工作。该数组表示法很简单,它使你的code。与minifiers兼容,所以我不知道为什么你不使用它。

这篇关于$注入上angularjs注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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