8
8
In AngularJS, a Controller is defined by a JavaScript **constructor function** that is used to augment the
9
9
{@link scope AngularJS Scope}.
10
10
11
- When a Controller is attached to the DOM via the {@link ng.directive:ngController ng-controller}
12
- directive, AngularJS will instantiate a new Controller object, using the specified Controller's
13
- **constructor function**. A new **child scope** will be created and made available as an injectable
14
- parameter to the Controller's constructor function as `$scope`.
11
+ Controllers can be attached to the DOM in different ways. For each of them, AngularJS will
12
+ instantiate a new Controller object, using the specified Controller's **constructor function**:
13
+
14
+ - the {@link ng.directive:ngController ngController} directive. A new **child scope** will be
15
+ created and made available as an injectable parameter to the Controller's constructor function
16
+ as `$scope`.
17
+ - a route controller in a {@link ngRoute.$routeProvider $route definition}.
18
+ - the controller of a {@link guide/directive regular directive}, or a
19
+ {@link guide/component component directive}.
15
20
16
21
If the controller has been attached using the `controller as` syntax then the controller instance will
17
- be assigned to a property on the new scope.
22
+ be assigned to a property on the scope.
18
23
19
24
Use controllers to:
20
25
@@ -33,6 +38,15 @@ Do not use controllers to:
33
38
services} instead.
34
39
- Manage the life-cycle of other components (for example, to create service instances).
35
40
41
+ In general, a Controller shouldn't try to do too much. It should contain only the business logic
42
+ needed for a single view.
43
+
44
+ The most common way to keep Controllers slim is by encapsulating work that doesn't belong to
45
+ controllers into services and then using these services in Controllers via dependency injection.
46
+ This is discussed in the {@link di Dependency Injection} and {@link services
47
+ Services} sections of this guide.
48
+
49
+
36
50
## Setting up the initial state of a `$scope` object
37
51
38
52
Typically, when you create an application you need to set up the initial state for the AngularJS
@@ -102,23 +116,6 @@ objects (or primitives) assigned to the scope become model properties. Any metho
102
116
the scope are available in the template/view, and can be invoked via AngularJS expressions
103
117
and `ng` event handler directives (e.g. {@link ng.directive:ngClick ngClick}).
104
118
105
- ## Using Controllers Correctly
106
-
107
- In general, a Controller shouldn't try to do too much. It should contain only the business logic
108
- needed for a single view.
109
-
110
- The most common way to keep Controllers slim is by encapsulating work that doesn't belong to
111
- controllers into services and then using these services in Controllers via dependency injection.
112
- This is discussed in the {@link di Dependency Injection} and {@link services
113
- Services} sections of this guide.
114
-
115
-
116
- # Associating Controllers with AngularJS Scope Objects
117
-
118
- You can associate Controllers with scope objects implicitly via the {@link ng.directive:ngController ngController
119
- directive} or {@link ngRoute.$route $route service}.
120
-
121
-
122
119
## Simple Spicy Controller Example
123
120
124
121
To illustrate further how Controller components work in AngularJS, let's create a little app with the
@@ -262,7 +259,7 @@ Inheritance works with methods in the same way as it does with properties. So in
262
259
examples, all of the properties could be replaced with methods that return string values.
263
260
264
261
265
- # Testing Controllers
262
+ ## Testing Controllers
266
263
267
264
Although there are many ways to test a Controller, one of the best conventions, shown below,
268
265
involves injecting the {@link ng.$rootScope $rootScope} and {@link ng.$controller $controller}:
0 commit comments