1

I'm new to Angular JS and in a tutorial they place that the way to create a controller is:

angular.module('app', [])
  .controller('TodoController', ['$scope', function ($scope) {
    $scope.todos = [
      { title: 'Learn Javascript', completed: true },
      { title: 'Learn Angular.js', completed: false },
      { title: 'Love this tutorial', completed: true },
      { title: 'Learn Javascript design patterns', completed: false },
      { title: 'Build Node.js backend', completed: false },
    ];
  }]);

I want to UNDERSTAND what does each of the parameters is:

  • 'TodoController'
  • array
    • '$scope'
    • function

I guess the first one is the name of the controller, and the last one is the TodoController constructor.

But what is '$scope' ? A variable name to use on the HTML, a method name?

Can I send more parameters in the array?

I searched on Angular docs but it is pretty lame with no doc about methods. Searching class code neither gave much more info.

2
  • 1
    i recomend to do the tutorial from codeschool.com/pages/angularjs-vs-angular where they are describing all concepts. Commented Jul 27, 2017 at 17:36
  • great explanation in that link @estus Commented Jul 27, 2017 at 21:04

3 Answers 3

2

In the array the arguments (String) are (excluding the last, yes that's the constructor) the dependencies that need to be injected in the sequence you have in your array, those will be passed in the same order in the implementation of the controller (i.e. the constructor).

$scope is a object, that maintains the binding with the view, and its contents are available both in view and controller. Its is getting created by the $new method of the ($rootScope and then the hierarchy) whenever you are injecting it in a new controller and instance of that controller is need to be created.

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

Comments

1

The second argument (Array of strings + function) is used for dependency injection.

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

In case your code gets minified angular will know which dependencies to inject, as strings are not affected by minification.

So, the above code after minification will become something like this:

angular.controller("TodoController",["$scope",function(o){...}]);

And it will still be readable by angular's dependency injection algorithm.

PS. Array is optional, you can pass there just plain function.

Comments

1

Yes. You are right.

First parameter is controller name. The second one array where last element in array will be controller function and other elements are dependencies. This is called Inline Array Annotation.

You can create the controller without passing array unless you are not minifying your code(Implicit annotation.)

Careful: If you plan to minify your code, your service names will get renamed and break your app.

Please check three ways of creating controllers or services. https://docs.angularjs.org/guide/di

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.