0

I made a little fiddle here. In the example below, there are 3 controllers:

1st is a regular controller.

2nd I changed ordering of controller constructor parameters

3rd Assigned value to local $scope instead of $rootScope.


2nd controller shows that even I when changed ordering and number of parameters, value still gets assigned to root scope. From this I conclude angular somehow knows names of parameters for a function, regardless of their order? (kinda like named parameters?) This sounds weird to me. How can parameter names mean something outside of function's own 'curly brackets'?

3rd controller shows, all local scopes are a copy of $rootScope, and {{variable}} convention points to variable inside local scope, instead of the global root scope.

Am I right in these two conclusions?

index.html :

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style.css">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
    <script src="script.js"></script>
  </head>

  <body ng-app="myApp" ng-controller="myController">

    <div ng-controller="myController">
      <h3>Echo: {{one}}</h3>
    </div>

    <div ng-controller="myController2">
      <h3>Echo: {{two}}</h3>
      <h3>Echo: {{three}}</h3>
    </div>

    <div ng-controller="myController3">
      <h3>Echo: {{three}}</h3>
    </div>

</body>
</html>

script.js

var app = angular.module('myApp',[]);
app.run(function($rootScope) {
    $rootScope.today = new Date();
});
app.controller("myController", function($rootScope, $scope){
    $rootScope.one = 'root scope one';
});
app.controller("myController2", function($scope, $scope, $rootScope){
    $rootScope.two = 'root scope two';
});
app.controller("myController3", function($scope, $scope, $rootScope){
    $scope.three = '~scope three';
});

outputs:

Echo: root scope one

Echo: root scope two

Echo:

Echo: ~scope three
1
  • 1
    Yes, angular dependency injection uses parameter names, unless you use the alternative array syntax, or the $inject field. This is all covered in the manual: docs.angularjs.org/guide/di Commented Nov 19, 2015 at 17:17

1 Answer 1

1
  1. Angular's dependency injection can infer the service to be injected based on the name. So yes, if you name the parameters according to the name of the services, then Angular will infer the correct service and the parameter ordering is irrelevant. This is not minification-safe, if you will be minifying your code you need to use one of the other approaches for injection. See section on Implicit Annotation in Angular's guide to Dependency Injection.

  2. There is a single $rootScope which is injected into each of your controllers, so they are not copies of each other but are the same object. Local scopes inherit from $rootScope via prototypical inheritance. Therefore, if you attempt to access a property on a local scope, Angular will first check on the local scope for the property but if it does not exist, it will search the parent scope (then the parent scope of the parent scope etc.) See Angular's guide to Scope especially the section on Scope Hierarchies.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.