我是 AngularJS 的新手。我想知道有什么区别$scope
在 angularjs 控制器中和scope
在 angularjs 指令中。
我尝试在控制器中使用范围,但出现以下错误:
错误:[$injector:unpr] 未知提供程序:scopeProvider
$scope
是由以下机构提供的服务$scopeProvider
。您可以使用 Angular 的内置依赖注入器将其注入控制器、指令或其他服务中:
module.controller(function($scope) {...})
这是简写
module.controller(['$scope', function($scope) {...}])
在第一个版本中,依赖注入器infers基于函数参数名称(“$scope”+“Provider”)的提供程序名称(“$scopeProvider”)。
第二个版本也像这样构建提供者名称,但使用explicit '$scope'
在数组中,而不是函数参数名称。这意味着您可以使用任何参数名称而不是$scope
.
因此你最终会得到这样的代码:module.controller(['$scope', function(scope) {...}])
where scope
可以是任何东西,它是一个函数参数名称,可以是foo
or a12342saa
.
依赖注入器基本上是这样做的:
function controller(def) {
//def[def.length-1] is the actual controller function
// everything before are it's dependencies
var dependencies = [];
for(dep in def.slice(0, def.length-1)) {
dependencies.push(__get_dependency_by_name(dep));
}
def[def.length-1].apply(dependencies);
}
我认为使用“scope”而不是“$scope”作为依赖项名称不起作用的原因现在已经很清楚了。没有定义“scopeProvider”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)