简体   繁体   English

角度服务注入

[英]Angular Service Injection

I am working on a trivial task (that I got working) which adds an an .active class to dom elements in a tab nav. 我正在开展一项简单的任务(我开始工作),它将一个.active类添加到选项卡导航中的dom元素。 Simple. 简单。 Got it working by following https://stackoverflow.com/a/12306136/2068709 . 通过以下https://stackoverflow.com/a/12306136/2068709了解它

Reading over the answer provided and comparing their controller to example controllers on angular's website I came across something interesting that I don't quite understand. 阅读所提供的答案,并将他们的控制器与angular网站上的示例控制器进行比较,我发现了一些我不太了解的有趣内容。

On the AngularJS website, $scope and $location (and other services) are injected into the controller along with an anonymous function which defines the controller. 在AngularJS网站上, $scope$location (以及其他服务)与定义控制器的匿名函数一起注入控制器。 But on the answer, the services are not injected , rather they are passed via the anonymous function. 但是在答案中,服务不会被注入 ,而是通过匿名函数传递 Why does this work? 为什么这样做? Shouldn't the service always be injected? 不应该始终注入服务吗?

In terms of an example: Why does this 就一个例子而言: 为什么这样

angular.module('controllers', [])
    .controller('NavigationController', ['$scope', '$location', function($scope, $location) { 
        $scope.isActive = function(route) {
            return route === $location.path();
        };
    }])

and this 还有这个

angular.module('controllers', [])
    .controller('NavigationController', function($scope, $location) { 
        $scope.isActive = function(route) {
            return route === $location.path();
        };
    })

both work? 都工作?

This may be trivial but its throwing my brain for a loop that I can't figure out. 这可能是微不足道的,但它把我的大脑扔到了一个我无法弄清楚的循环中。

The two examples are equivalent - they just make use of different syntax. 这两个例子是等价的 - 它们只是使用不同的语法。 The first example uses what they call "inline array annotation" (see here ). 第一个例子使用他们所谓的“内联数组注释”(见这里 )。 The purpose of this alternate syntax is just to allow a convenient way to make the injected variable names different than the name of the dependency. 此替代语法的目的是允许一种方便的方法使注入的变量名称与依赖项的名称不同。

So for example, if you wanted the variable names to be "s" and "l", then you could write: 因此,例如,如果您希望变量名称为“s”和“l”,那么您可以编写:

angular.module('controllers', [])
    .controller('NavigationController', ['$scope', '$location', function(s, l) { 
        s.isActive = function(route) {
            return route === l.path();
        };
    }]);

Actually they are injected in both cases, the difference between those two cases is in the first scenario you define and name the dependency this could be useful if you minify your js code and that way you are declaring explicitly the dependency for examply it could be: 实际上它们是在两种情况下注入的,这两种情况之间的差异在你定义的第一个场景中,并且命名依赖项,如果你缩小你的js代码,这可能是有用的,并且你明确地声明了依赖,例如它可能是:

angular.module('controllers', [])
    .controller('NavigationController', ['$scope', '$location', function($s, $l) { 
        $s.isActive = function(route) {
            return route === $l.path();
        };
    }])

that way angular will know which dependency to inject on which parameter without looking at the naming for each parameter. 这样,angular将知道注入哪个参数的依赖关系而不查看每个参数的命名。

the other case you need to be explicit and declare which dependency you'll inject by setting up the name. 在另一种情况下,您需要明确并通过设置名称来声明您将注入哪个依赖项。

I hope that helps. 我希望有所帮助。

This is how angular handles code minification. 这就是角度处理代码缩小的方式。 by keeping strings intact it can keep mapping vars when they are renamed. 通过保持字符串完整,它可以在重命名时保持映射变量。 if you take a look at the code of the controller https://github.com/angular/angular.js/blob/master/src/ng/controller.js#L48 you'll see that the constructor can accept both function and array. 如果你看一下控制器的代码https://github.com/angular/angular.js/blob/master/src/ng/controller.js#L48,你会发现构造函数可以接受函数和阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM