Angularjs:$scope 与作用域 [英] Angularjs: $scope vs scope

查看:26
本文介绍了Angularjs:$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.

对于指令的情况,注入是基于位置的,所以你可以命名你的变量 ab 或任何东西.链接函数的指令顺序是

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屋!

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