@@ -103,23 +103,25 @@ string "very". Depending on which button is clicked, the `spice` model is set to
103
103
## A Spicy Controller Example
104
104
105
105
<pre>
106
- <body ng-controller="SpicyCtrl">
106
+ <body ng-app="SpicyApp" ng- controller="SpicyCtrl">
107
107
<button ng-click="chiliSpicy()">Chili</button>
108
108
<button ng-click="jalapenoSpicy()">Jalapeño</button>
109
109
<p>The food is {{spice}} spicy!</p>
110
110
</body>
111
111
112
- function SpicyCtrl($scope) {
113
- $scope.spice = 'very';
114
- $scope.chiliSpicy = function() {
115
- $scope.spice = 'chili';
116
- }
117
- $scope.jalapenoSpicy = function() {
118
- $scope.spice = 'jalapeño';
119
- }
120
- }
121
-
122
-
112
+ var myApp = angular.module('SpicyApp', []);
113
+
114
+ myApp.controller('SpicyCtrl', ['$scope', function($scope){
115
+ $scope.spicy = 'very';
116
+
117
+ $scope.chiliSpicy = function() {
118
+ $scope.spice = 'chili';
119
+ };
120
+
121
+ $scope.jalapenoSpicy = function() {
122
+ $scope.spice = 'jalapeño';
123
+ };
124
+ }]);
123
125
</pre>
124
126
125
127
Things to notice in the example above:
@@ -147,19 +149,24 @@ previous example.
147
149
## Controller Method Arguments Example
148
150
149
151
<pre>
150
- <body ng-controller="SpicyCtrl">
151
- <input ng-model="customSpice" value="wasabi" >
152
+ <body ng-app="SpicyApp" ng- controller="SpicyCtrl">
153
+ <input ng-model="customSpice">
152
154
<button ng-click="spicy('chili')">Chili</button>
153
155
<button ng-click="spicy(customSpice)">Custom spice</button>
154
156
<p>The food is {{spice}} spicy!</p>
155
157
</body>
156
158
157
- function SpicyCtrl($scope) {
158
- $scope.spice = 'very';
159
- $scope.spicy = function(spice) {
160
- $scope.spice = spice;
161
- }
162
- }
159
+ var myApp = angular.module('SpicyApp', []);
160
+
161
+ myApp.controller('SpicyCtrl', ['$scope', function($scope){
162
+ $scope.customSpice = "wasabi";
163
+ $scope.spice = 'very';
164
+
165
+ $scope.spicy = function(spice){
166
+ $scope.spice = spice;
167
+ };
168
+ }]);
169
+
163
170
</pre>
164
171
165
172
Notice that the `SpicyCtrl` controller now defines just one method called `spicy`, which takes one
@@ -174,27 +181,27 @@ Controller inheritance in Angular is based on {@link api/ng.$rootScope.Scope Sco
174
181
have a look at an example:
175
182
176
183
<pre>
177
- <body ng-controller="MainCtrl">
184
+ <body ng-app="MyApp" ng- controller="MainCtrl">
178
185
<p>Good {{timeOfDay}}, {{name}}!</p>
179
186
<div ng-controller="ChildCtrl">
180
187
<p>Good {{timeOfDay}}, {{name}}!</p>
181
188
<p ng-controller="BabyCtrl">Good {{timeOfDay}}, {{name}}!</p>
182
189
</div>
183
190
</body>
184
191
185
- function MainCtrl($scope) {
186
- $scope.timeOfDay = 'morning';
187
- $scope.name = 'Nikki';
188
- }
189
-
190
- function ChildCtrl($scope) {
191
- $scope.name = 'Mattie';
192
- }
193
-
194
- function BabyCtrl($scope) {
195
- $scope.timeOfDay = 'evening';
196
- $scope.name = 'Gingerbreak Baby';
197
- }
192
+ var myApp = angular.module('MyApp', [])
193
+
194
+ .controller('MainCtrl', [' $scope', function($scope){
195
+ $scope.timeOfDay = 'morning';
196
+ $scope.name = 'Nikki';
197
+ }])
198
+ .controller('ChildCtrl', [' $scope', function($scope){
199
+ $scope.name = 'Mattie';
200
+ }])
201
+ .controller(' BabyCtrl', ['$scope', function ($scope){
202
+ $scope.timeOfDay = 'evening';
203
+ $scope.name = 'Gingerbreak Baby';
204
+ }]);
198
205
</pre>
199
206
200
207
Notice how we nested three `ngController` directives in our template. This template construct will
0 commit comments