admin管理员组

文章数量:1410705

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>

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>
Share Improve this question asked Mar 28, 2014 at 2:19 KacyKacy 3,4305 gold badges33 silver badges61 bronze badges 1
  • Dependency Injection AND the fact that the injection is based on parameter name. (Many tend to forgot to mention that last detail.) – Johan Commented Nov 7, 2016 at 14:45
Add a ment  | 

3 Answers 3

Reset to default 6

This is handled by the Angular injector.

http://docs.angularjs/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/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.

Both previous answers are correct , just you should know that you can "override" the default behavior if you're declaring a controller this way:

module.controller("ControllerName",["$scope",function( custom_name ){ ... }]);

Example:

var app = angular.module("myApp",[]);

app.controller("TextController",["$scope",function(glue){
    glue.name1 = "John";
    glue.name2 = "Paul";
    glue.name3 = "George";
    glue.name4 = "Ringo";
}]);

and then:

<div ng-controller="TextController">
    Hello {{ name1 }}, {{ name2 }}, {{ name3 }}, {{ name4 }}!
</div>

Working here: http://jsfiddle/d4M2P/

Because the Angular uses the name of the parameter in its dependency injection system, which constructs the controller.

本文标签: javascriptWhy is the variable name quotscopequot necessary Stack Overflow