理解依赖注入在AngularJS控制器 [英] Understanding dependency injection in AngularJS controllers

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

问题描述

刚学依赖注入,我想我开始了解它。

请告诉我,如果我在正确的轨道上......

例如:这些是两种等价?

  / *注射法* /
功能<&CONTROLLER_NAME GT;($范围){}
< CONTROLLER_NAME方式> $注射='$范围'];/ *其他方法* /
VAR应用= angular.module('对myApp');
app.controller(小于CONTROLLER_NAME>中的功能($范围){});


解决方案

首先一点澄清:

有关依赖注入,这不要紧,你是否使用声明全局函数或控制器 module.controller(...)方法的参数。依赖注入是只关注功能本身。所以,你实际上问的是这两者的等价:

  //第一功能myController的($范围){}。$ myController的注入='$范围'];//二功能($范围){}

和因为无论控制器功能是匿名的,或还没有为注射器不要紧,上述两个本来也可能是:

  //第一功能myController的($范围){}。$ myController的注入='$范围'];//二功能myController的($范围){}

现在很明显,你的两个控制器之间的唯一区别是的presence的 $注资在其中的一个属性。

而这里的实际回答你的问题:

这两个控制器是几乎相同。双方将收到 $范围作为参数,将功能相同。但是,如果您决定稍后来缩小您的code,只能用 $版本注入阵列设置将正常工作。这是因为,如果不指定 $注阵列,也不能使用内嵌批注的方法(<一个href=\"http://docs.angularjs.org/guide/di#inlineannotation\">http://docs.angularjs.org/guide/di#inlineannotation),用于注射器的唯一办法,找出哪些依赖你感兴趣的是检查你函数的参数(其当成服务ID)的名称,但缩小会说出这些参数随机从而消除认识到依赖这样的机会。

所以,如果你要来缩小您的code,你必须明确使用 $注数组或内嵌批注指定的依赖,否则,任何版本会工作一样好。

Just learning dependency injection, and I think I'm starting to understand it.

Please tell me if I'm on the right track...

E.g.: Are these two equivalent?

/* injection method */
function <controller_name>($scope) {}
<controller_name>.$inject = ['$scope'];

/* other method */
var app = angular.module('myApp');
app.controller(<controller_name>, function($scope) {});

解决方案

First a little clarification:

For dependency injection, it doesn't matter whether you declare a controller using a global function or as the argument of module.controller(...) method. Dependency injector is only concerned about the function itself. So what you're actually asking about is the equivalence of those two:

// First

function MyController($scope) {}

MyController.$inject = [ '$scope '];

// Second

function($scope) {}

And because whether the controller function is anonymous or not also doesn't matter for the injector, the above two can just as well be:

// First

function MyController($scope) {}

MyController.$inject = [ '$scope '];

// Second

function MyController($scope) {}

Now it's clear that the only difference between your two controllers is the presence of the $inject property in one of them.

And here's the actual answer to your question:

These two controllers are almost the same. Both will receive the $scope as the argument and will function the same. However, if you decide to minify your code later, only the version with $inject array set on it will work properly. This is because if you don't specify the $inject array nor use the inline annotation approach (http://docs.angularjs.org/guide/di#inlineannotation), the only way for the injector to find out which dependencies you were interested in is to check the names of your function arguments (treating them as service IDs). But minification would name those arguments randomly thus removing the chance to recognize dependencies this way.

So if you're going to minify your code, you have to specify the dependencies explicitly using $inject array or inline annotation, otherwise, any version will work just as good.

这篇关于理解依赖注入在AngularJS控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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