简体   繁体   English

angularjs,$ scope参数在后台如何工作?

[英]angularjs, how $scope parameter works behind the scenes?

Controllers take as a second parameter an anonymous function. 控制器将匿名函数作为第二个参数。

I have two examples of controllers, one with the anonymous function taking as parameter nothing , and the second taking the $scope object. 我有两个控制器示例,一个使用匿名函数将nothing作为参数,第二个使用$scope对象。

How angularjs "behind the scenes" can detect that my anonymous function has an argument or not? “位于幕后”的angularjs如何检测我的匿名函数是否带有参数? And how can I mimic that in a custom js method? 以及如何在自定义js方法中模仿呢?

controller('ctrl',function(){
        console.log( arguments[0] ); // ->> undefined
    });

vs VS

controller('ctrl',function($scope){
        console.log( arguments[0] ); // ->> js object
    });

A pseudo code that I`m thinking is like this: 我在想的伪代码是这样的:

( if F has parameters ) F.bind($scope) // or F.call(....)

As documented Here 作为记录在这里

Angular will instantiate a new Controller object, using the specified Controller's constructor function. Angular将使用指定的Controller的构造函数实例化一个新的Controller对象。 A new child scope will be created and made available as an injectable parameter to the Controller's constructor function as $scope. 将创建一个新的子作用域,并将其作为可注入参数提供给Controller的构造函数$ scope。

So the first argument passed to the Controller's constructor function is the child scope instance $scope. 因此,传递给Controller的构造函数的第一个参数是子作用域实例$ scope。 Since the arguments object is an Array-like object corresponding to the arguments passed to a function, printing arguments[0] will print the first passed argument (ie $scope). 由于arguments对象是与传递给函数的参数相对应的类似Array的对象,因此打印arguments [0]将打印第一个传递的参数(即$ scope)。

It is how angular's Dependency Injection works: it looks at the args names, then looks it up to know what to inject. 这是angular的依赖注入的工作方式:它查看args名称,然后查找它以知道要注入什么。

You can find the specific code for this here: 您可以在此处找到特定的代码:

https://github.com/angular/angular.js/blob/master/src/auto/injector.js#L72 https://github.com/angular/angular.js/blob/master/src/auto/injector.js#L72

Angular gets the names of the objects to inject from one of three places (only the first of these that exists is used): Angular从三个位置之一获取要注入的对象的名称(仅使用存在的对象中的第一个):

  • The $inject attribute of the function 函数的$inject属性
  • If instead of a function you give it an array then the function should be the last element of the array and the injectable names are all the others. 如果给您一个函数而不是给它一个数组,则该函数应该是数组的最后一个元素,并且所有其他可注入名称。
  • Otherwise it inspects the arguments the function is actually expecting by calling toString() on the function and extracting the names from that. 否则,它通过在函数上调用toString()并从中提取名称来检查该函数实际期望的参数。

Once it has a list of names the injector will find objects first in a mapping object that may be passed to the injector and then in the objects that have been registered in the available angular modules. 一旦具有名称列表,注入器将首先在可传递给注入器的映射对象中找到对象,然后在已注册到可用角度模块中的对象中找到对象。 Again the first matching object is used, so the code to invoke a controller passes a unique $scope object to the injector for each controller but most other names you can use such as for services are singletons and the same object is reused each time. 再次使用第一个匹配对象,因此调用控制器的代码将唯一的$scope对象传递给每个控制器的注入器,但是您可以使用的大多数其他名称(例如,用于服务)都是单例,并且每次都重复使用同一对象。

You can use the injector yourself: 您可以自己使用注射器:

var injector = angular.injector();
function MyFunction(something) { ... }
injector.invoke(MyFunction, this, { something: anObject });

See https://docs.angularjs.org/api/auto/service/ $injector 参见https://docs.angularjs.org/api/auto/service/ $ injector

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

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