forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_guide.mvc.understanding_controller.html
241 lines (192 loc) · 12 KB
/
dev_guide.mvc.understanding_controller.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<h1>Developer Guide: About MVC in Angular: Understanding the Controller Component</h1>
<div class="developer-guide-about-mvc-in-angular-understanding-the-controller-component"><fieldset class="workInProgress"><legend>Work in Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.</fieldset>
<p>In angular, a controller is a JavaScript function (type/class) that is used to augment instances of
angular <a href="#!/guide/dev_guide.scopes">Scope</a>, excluding the root scope. When you or angular create a new
child scope object via the <a href="#!/api/angular.scope.$new"><code>scope.$new</code></a> API , there is an
option to pass in a controller as a method argument. This will tell angular to associate the
controller with the new scope and to augment its behavior.</p>
<p>Use controllers to:</p>
<ul>
<li>Set up the initial state of a scope object.</li>
<li>Add behavior to the scope object.</li>
</ul>
<h2>Setting up the initial state of a scope object</h2>
<p>Typically, when you create an application you need to set up an initial state for an angular scope.</p>
<p>Angular applies (in the sense of JavaScript's <code>Function#apply</code>) the controller constructor function
to a new angular scope object, which sets up an initial scope state. This means that angular never
creates instances of the controller type (by invoking the <code>new</code> operator on the controller
constructor). Constructors are always applied to an existing scope object.</p>
<p>You set up the initial state of a scope by creating model properties. For example:</p>
<p>function GreetingCtrl() {
this.greeting = 'Hola!';
}</p>
<p>The <code>GreetingCtrl</code> controller creates a <code>greeting</code> model which can be referred to in a template.</p>
<p>When a controller function is applied to an angular scope object, the <code>this</code> of the controller
function becomes the scope of the angular scope object, so any assignment to <code>this</code> within the
controller function happens on the angular scope object.</p>
<h2>Adding Behavior to a Scope Object</h2>
<p>Behavior on an angular scope object is in the form of scope method properties available to the
template/view. This behavior interacts with and modifies the application model.</p>
<p>As discussed in the <a href="#!/guide/dev_guide.mvc.understanding_model">Model</a> section of this guide, any
objects (or primitives) assigned to the scope become model properties. Any functions assigned to
the scope, along with any prototype methods of the controller type, become functions available in
the template/view, and can be invoked via angular expressions and <code>ng:</code> event handlers (e.g. <a href="#!/api/angular.directive.ng:click"><code>ng:click</code></a>). These controller methods are always evaluated within the
context of the angular scope object that the controller function was applied to (which means that
the <code>this</code> keyword of any controller method is always bound to the scope that the controller
augments). This is how the second task of adding behavior to the scope is accomplished.</p>
<h2>Using Controllers Correctly</h2>
<p>In general, a controller shouldn't try to do too much. It should contain only the business logic
needed for a single view.</p>
<p>The most common way to keep controllers slim is by encapsulating work that doesn't belong to
controllers into services and then using these services in controllers via dependency injection.
This is discussed in the <a href="#!/guide/dev_guide.di">Dependency Injection</a> <a href="#!/guide/dev_guide.services">Services</a> sections of this guide.</p>
<p>Do not use controllers for:</p>
<ul>
<li>Any kind of DOM manipulation â Controllers should contain only business logic. DOM
manipulationâthe presentation logic of an applicationâis well known for being hard to test.
Putting any presentation logic into controllers significantly affects testability of the business
logic. Angular offers <a href="#!/guide/dev_guide.templates.databinding">dev_guide.templates.databinding</a> for automatic DOM manipulation. If
you have to perform your own manual DOM manipulation, encapsulate the presentation logic in <a href="#!/guide/dev_guide.compiler.widgets">widgets</a> and <a href="#!/guide/dev_guide.compiler.directives">directives</a>.</li>
<li>Input formatting â Use <a href="#!/guide/dev_guide.templates.formatters">angular formatters</a> instead.</li>
<li>Output filtering â Use <a href="#!/guide/dev_guide.templates.filters">angular filters</a> instead.</li>
<li>Run stateless or stateful code shared across controllers â Use <a href="#!/guide/dev_guide.services">angular services</a> instead.</li>
<li>Instantiate or manage the life-cycle of other components (for example, to create service
instances).</li>
</ul>
<h2>Associating Controllers with Angular Scope Objects</h2>
<p>You can associate controllers with scope objects explicitly via the <a href="#!/api/angular.scope.$new"><code>scope.$new</code></a> api or implicitly via the <a href="#!/api/angular.directive.ng:controller"><code>ng:controller directive</code></a> or <a href="#!/api/angular.service.$route"><code>$route service</code></a>.</p>
<h3>Controller Constructor and Methods Example</h3>
<p>To illustrate how the controller component works in angular, let's create a little app with the
following components:</p>
<ul>
<li>A <a href="#!/guide/dev_guide.templates">template</a> with two buttons and a simple message</li>
<li>A model consisting of a string named <code>spice</code></li>
<li>A controller with two functions that set the value of <code>spice</code></li>
</ul>
<p>The message in our template contains a binding to the <code>spice</code> model, which by default is set to the
string "very". Depending on which button is clicked, the <code>spice</code> model is set to <code>chili</code> or
<code>jalapeño</code>, and the message is automatically updated by data-binding.</p>
<h3>A Spicy Controller Example</h3><div ng:non-bindable><pre class="brush: js; html-script: true;">
<body ng:controller="SpicyCtrl">
<button ng:click="chiliSpicy()">Chili</button>
<button ng:click="jalapenoSpicy()">Jalapeño</button>
<p>The food is {{spice}} spicy!</p>
</body>
function SpicyCtrl() {
this.spice = 'very';
this.chiliSpicy = function() {
this.spice = 'chili';
}
}
SpicyCtrl.prototype.jalapenoSpicy = function() {
this.spice = 'jalapeño';
}
</pre></div><p>Things to notice in the example above:</p>
<ul>
<li>The <code>ng:controller</code> directive is used to (implicitly) create a scope for our template, and the
scope is augmented (managed) by the <code>SpicyCtrl</code> controller.</li>
<li><code>SpicyCtrl</code> is just a plain JavaScript function. As an (optional) naming convention the name
starts with capital letter and ends with "Ctrl" or "Controller".</li>
<li>The JavaScript keyword <code>this</code> in the <code>SpicyCtrl</code> function is bound to the scope that the
controller augments.</li>
<li>Assigning a property to <code>this</code> creates or updates the model.</li>
<li>Controller methods can be created through direct assignment to scope (the <code>chiliSpicy</code> method) or
as prototype methods of the controller constructor function (the <code>jalapenoSpicy</code> method)</li>
<li>Both controller methods are available in the template (for the <code>body</code> element and and its
children).</li>
</ul>
<p>Controller methods can also take arguments, as demonstrated in the following variation of the
previous example.</p>
<h3>Controller Method Arguments Example</h3><div ng:non-bindable><pre class="brush: js; html-script: true;">
<body ng:controller="SpicyCtrl">
<input name="customSpice" value="wasabi">
<button ng:click="spicy('chili')">Chili</button>
<button ng:click="spicy(customSpice)">Custom spice</button>
<p>The food is {{spice}} spicy!</p>
</body>
function SpicyCtrl() {
this.spice = 'very';
this.spicy = function(spice) {
this.spice = spice;
}
}
</pre></div><p>Notice that the <code>SpicyCtrl</code> controller now defines just one method called <code>spicy</code>, which takes one
argument called <code>spice</code>. The template then refers to this controller method and passes in a string
constant <code>'chili'</code> in the binding for the first button and a model property <code>spice</code> (bound to an
input box) in the second button.</p>
<h3>Controller Inheritance Example</h3>
<p>Controller inheritance in angular is based on <a href="#!/api/angular.scope"><code>Scope</code></a> inheritance. Let's
have a look at an example:</p><div ng:non-bindable><pre class="brush: js; html-script: true;">
<body ng:controller="MainCtrl">
<p>Good {{timeOfDay}}, {{name}}!</p>
<div ng:controller="ChildCtrl">
<p>Good {{timeOfDay}}, {{name}}!</p>
<p ng:controller="BabyCtrl">Good {{timeOfDay}}, {{name}}!</p>
</body>
function MainCtrl() {
this.timeOfDay = 'morning';
this.name = 'Nikki';
}
function ChildCtrl() {
this.name = 'Mattie';
}
function BabyCtrl() {
this.timeOfDay = 'evening';
this.name = 'Gingerbreak Baby';
}
</pre></div><p>Notice how we nested three <code>ng:controller</code> directives in our template. This template construct will
result in 4 scopes being created for our view:</p>
<ul>
<li>The root scope</li>
<li>The <code>MainCtrl</code> scope, which contains <code>timeOfDay</code> and <code>name</code> models</li>
<li>The <code>ChildCtrl</code> scope, which shadows the <code>name</code> model from the previous scope and inherits the
<code>timeOfDay</code> model</li>
<li>The <code>BabyCtrl</code> scope, which shadows both the <code>timeOfDay</code> model defined in <code>MainCtrl</code> and <code>name</code>
model defined in the ChildCtrl</li>
</ul>
<p>Inheritance works between controllers in the same way as it does with models. So in our previous
examples, all of the models could be replaced with controller methods that return string values.</p>
<p>Note: Standard prototypical inheritance between two controllers doesn't work as one might expect,
because as we mentioned earlier, controllers are not instantiated directly by angular, but rather
are applied to the scope object.</p>
<h3>Testing Controllers</h3>
<p>The way to test a controller depends upon how complicated the controller is.</p>
<ul>
<li>If your controller doesn't use DI or scope methods â create the controller with the <code>new</code>
operator and test away. For example:</li>
</ul>
<p>Controller Function:</p><div ng:non-bindable><pre class="brush: js;">
function myController() {
this.spices = [{"name":"pasilla", "spiciness":"mild"},
{"name":"jalapeno", "spiceiness":"hot hot hot!"},
{"name":"habanero", "spiceness":"LAVA HOT!!"}];
this.spice = "habanero";
}
</pre></div><p>Controller Test:</p><div ng:non-bindable><pre class="brush: js;">
describe('myController function', function() {
describe('myController', function(){
var ctrl;
beforeEach(function() {
ctrl = new myController();
});
it('should create "spices" model with 3 spices', function() {
expect(ctrl.spices.length).toBe(3);
});
it('should set the default value of spice', function() {
expect(ctrl.spice).toBe('habanero');
});
});
});
</pre></div><ul>
<li>If your controller does use DI or scope methods â create a root scope, then create the controller
in the root scope with <code>scope.$new(MyController)</code>. Test the controller using <code>$eval</code>, if necessary.</li>
<li>If you need to test a nested controller that depends on its parent's state â create a root scope,
create a parent scope, create a child scope, and test the controller using $eval if necessary.</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="#!/guide/dev_guide.mvc">About MVC in Angular</a></li>
<li><a href="#!/guide/dev_guide.mvc.understanding_model">Understanding the Model Component</a></li>
<li><a href="#!/guide/dev_guide.mvc.understanding_view">Understanding the View Component</a></li>
</ul></div>