在angularjs $范围和范围的区别 [英] Difference between $scope and scope in angularjs

查看:210
本文介绍了在angularjs $范围和范围的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来angularjs。我想知道的是什么angularjs指令在angularjs控制器 $范围范围之间的差异。

我试图控制使用范围,我得到了下面的错误:


  

错误:[$喷油器:unpr]未知提供商:scopeProvider< - 范围



解决方案

$范围 $ scopeProvider 。你可以注射到控制器,指示或其他服务的采用了棱角分明的内置依赖注入:

module.controller(函数($范围内){...})

这是简写

module.controller(['$范围',函数($范围内){...}])

在第一个版本的依赖注入的推断的供应商的基础上,功能参数($范围+提供者)的名称名称($ scopeProvider)。
第二个版本还建立提供者的名称一样,但使用的明确'$范围在阵列中,而不是函数参数的名称。这意味着你可以使用任何参数的名称,而不是 $范围

于是你结束了code是这样的:
module.controller(['$范围,功能(适用范围){...}])
其中,范围可以是任何东西,它是一个函数参数名称,可能是 a12342saa

依赖注入基本上做到这一点:

 功能控制器(DEF){
    //def[def.length-1]为实际控制功能
    //之前的一切都是它的依赖    变种依赖= [];
    为(DEP中def.slice(0,def.length-1)){
         dependencies.push(__ get_dependency_by_name(DEP));
    }
    DEF [def.length-1]。适用(依赖关系);
}

我认为,之所以使用范围,而不是$范围作为一个依赖名称将不能正常工作,现在是明确的。有没有scopeProvider定义的。

I am new to angularjs. I would like to know what is the difference between $scope in angularjs Controller and scope in angularjs Directive.

I tried to use scope in controller and I got the below error:

Error: [$injector:unpr] Unknown provider: scopeProvider <- scope

解决方案

$scope is a service provided by $scopeProvider. You can inject it into controllers, directives or other services using Angular's built-in dependency injector:

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

which is shorthand for

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

In the first version the Dependency Injector infers the name of the provider ("$scopeProvider") based on the name of the function parameter ("$scope" + "Provider"). The second version also builds the provider name like that but uses the explicit '$scope' in the array, not the function parameter name. That means you can use any parameter name instead of $scope.

Thus you end up with code like this: module.controller(['$scope', function(scope) {...}]) where scope could be anything, it's a function parameter name, could be foo or a12342saa.

The dependency injector basically does this:

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);
}

I think the reason why using "scope" instead of "$scope" as a dependency name won't work is clear now. There's no "scopeProvider" defined.

这篇关于在angularjs $范围和范围的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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