Angularjs:$scope 与作用域 [英] Angularjs: $scope vs scope
问题描述
在 Angularjs 中,在控制器中使用 $scope
和在指令链接函数中使用 scope
(没有 "$"
)是否有特定的原因?这只是一个约定还是其他什么?
In Angularjs, is there a specific reason to use $scope
in controllers and scope
(without "$"
) in directives link function? Is it just a convention or anything else?
推荐答案
当你在控制器中做 $scope
的情况下,依赖注入会根据匹配的变量名 $scope<注入作用域/code>,在这种情况下,使用
scope
作为名称将不起作用.
The case when you do $scope
in controller the Dependency Injection injects scope based on matching the variable name $scope
, in this case using scope
as name would not work.
对于指令的情况,注入是基于位置的,所以你可以命名你的变量 a
或 b
或任何东西.链接函数的指令顺序是
For case of directive the injection is position based so you can name your variable a
or b
or any thing. The directive order for link function is
(scope, iElement, iAttrs, controller)
所以第一个元素总是作用域对象.
so first element is always scope object.
这篇关于Angularjs:$scope 与作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!