angularjs中$ scope和scope的区别

时间:2015-05-14 10:10:51

标签: angularjs angularjs-directive angularjs-scope

我是angularjs的新手。我想知道angularjs Controller中的$scope和angularjs指令中的scope之间有什么区别。

我尝试在控制器中使用范围,我得到以下错误:

  

错误:[$ injector:unpr]未知提供者:scopeProvider< - scope

1 个答案:

答案 0 :(得分:4)

$scope$scopeProvider提供的服务。您可以使用Angular的内置依赖注入器将其注入控制器,指令或其他服务中:

module.controller(function($scope) {...})

这是

的简写

module.controller(['$scope', function($scope) {...}])

在第一个版本中,Dependency Injector 根据函数参数的名称推断提供者的名称(" $ scopeProvider")(" $ scope&# 34; +"提供商")。 第二个版本也会像这样构建提供程序名称,但在数组中使用显式 '$scope',而不是函数参数名称。这意味着您可以使用任何参数名称而不是$scope

因此你最终得到这样的代码: module.controller(['$scope', function(scope) {...}]) scope可以是任何内容,它是一个函数参数名称,可以是fooa12342saa

依赖注入器基本上是这样做的:

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"作为依赖名称,现在已经很清楚了。没有" scopeProvider"定义

相关问题