Skip to content

Commit c302425

Browse files
felixcrivpetebacondarwin
authored andcommitted
docs(guide/controller): use .controller syntax
Use the recommended `module.controller` syntax rather than global functions to define controllers in the examples.
1 parent 770353d commit c302425

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,25 @@ string "very". Depending on which button is clicked, the `spice` model is set to
103103
## A Spicy Controller Example
104104

105105
<pre>
106-
<body ng-controller="SpicyCtrl">
106+
<body ng-app="SpicyApp" ng-controller="SpicyCtrl">
107107
<button ng-click="chiliSpicy()">Chili</button>
108108
<button ng-click="jalapenoSpicy()">Jalapeño</button>
109109
<p>The food is {{spice}} spicy!</p>
110110
</body>
111111

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+
}]);
123125
</pre>
124126

125127
Things to notice in the example above:
@@ -147,19 +149,24 @@ previous example.
147149
## Controller Method Arguments Example
148150

149151
<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">
152154
<button ng-click="spicy('chili')">Chili</button>
153155
<button ng-click="spicy(customSpice)">Custom spice</button>
154156
<p>The food is {{spice}} spicy!</p>
155157
</body>
156158

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+
163170
</pre>
164171

165172
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
174181
have a look at an example:
175182

176183
<pre>
177-
<body ng-controller="MainCtrl">
184+
<body ng-app="MyApp" ng-controller="MainCtrl">
178185
<p>Good {{timeOfDay}}, {{name}}!</p>
179186
<div ng-controller="ChildCtrl">
180187
<p>Good {{timeOfDay}}, {{name}}!</p>
181188
<p ng-controller="BabyCtrl">Good {{timeOfDay}}, {{name}}!</p>
182189
</div>
183190
</body>
184191

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+
}]);
198205
</pre>
199206

200207
Notice how we nested three `ngController` directives in our template. This template construct will

0 commit comments

Comments
 (0)