为什么变量名" $范围"有必要吗? [英] Why is the variable name "$scope" necessary ?

查看:119
本文介绍了为什么变量名" $范围"有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的JavaScript(刚刚读完这本书雄辩的Javascript),我目前由O'Reilly阅读AngularJS。并获得code这个小片段工作从书让我抓狂了几个小时,并导致我下来的兔子洞想我在设置我的环境中的某个地方搞砸了。
由AngularJS书和code我打完了所提供的code,唯一的区别是,我在TextController功能冷落在$范围的$。把$早在允许的code工作。

下面是我的推理最初离开吧:
哦,'$范围'只是局部的函数变量名。像任何其他的编程语言如Java或C ++,因为这个参数仅仅是一个局部变量,我可以命名它,因为无论参数被传递给函数只会得到按值传递我想做的事情。

请纠正我的推理和解释为什么参数的名称必须是$范围。

 <!DOCTYPE HTML>
< HTML NG-应用><机身NG控制器=TextController>  &所述p为H.; {{someText}}&下; / P>  &所述; SCRIPT SRC =angular.min.js>&下; /脚本>  <脚本>
    功能TextController($范围){
      $ scope.someText ='你已经开始你的旅程。;
    }
  < / SCRIPT>< /身体GT;< / HTML>


解决方案

这是由喷油角处理。

<一个href=\"http://docs.angularjs.org/api/auto/service/\">http://docs.angularjs.org/api/auto/service/$injector


  

在JavaScript调用toString()的函数返回函数定义。然后定义可以被解析和函数参数可以被提取。注意:因为这些工具改变参数名这不会缩小工作,和模糊处理工具


http://docs.angularjs.org/guide/di


  

给定一个功能的喷射器可以推断出该服务的名称通过检查函数声明和提取的参数名来注入。在上面的例子中$范围,招待员两种服务需要被注入的功能。


I'm fairly new to Javascript ( just finished the book Eloquent Javascript ), and am currently reading AngularJS from O'Reilly. And getting this small snippet of code to work from the book drove me crazy for hours and led me down rabbit holes thinking I messed up somewhere in setting up my environment. The only difference in the code provided by the AngularJS book and the code I typed up was that I left out the '$' in "$scope" in the TextController function. Putting the '$' back in allowed the code to work.

Here was my reasoning for initially leaving it out: Oh, '$scope' is just a variable name local to the function. Like any other programming language such as Java or C++, because this parameter is just a local variable, I can name it whatever I want since whatever argument gets passed into the function will just get passed by value.

Please correct my reasoning and explain why the name of the parameter has to be "$scope".

<!doctype html>
<html ng-app>

<body ng-controller="TextController">

  <p>{{someText}}</p>

  <script src="angular.min.js"></script>

  <script>
    function TextController($scope) {
      $scope.someText = 'You have started your journey.';
    }
  </script>

</body>

</html>

解决方案

This is handled by the Angular injector.

http://docs.angularjs.org/api/auto/service/$injector

In JavaScript calling toString() on a function returns the function definition. The definition can then be parsed and the function arguments can be extracted. NOTE: This does not work with minification, and obfuscation tools since these tools change the argument names.

http://docs.angularjs.org/guide/di

Given a function the injector can infer the names of the service to inject by examining the function declaration and extracting the parameter names. In the above example $scope, and greeter are two services which need to be injected into the function.

这篇关于为什么变量名&QUOT; $范围&QUOT;有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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