Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit be41adc

Browse files
cranesandcaffpetebacondarwin
authored andcommitted
docs(guide/controller): only show best practice controller creation
If it is not recommended to use a global function to create controllers, why should it be shown as possible in the documentation? One of the most common complaints about AngularJS is that it doesn't enforce any convention. This is intentional and I generally like this. However if we can avoid outright bad implementations in examples I believe we should. Closes #8011
1 parent 7e77521 commit be41adc

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

docs/content/guide/controller.ngdoc

+10-16
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ The properties contain the **view model** (the model that will be presented by t
3737
`$scope` properties will be available to the template at the point in the DOM where the Controller
3838
is registered.
3939

40-
The following example shows a very simple constructor function for a Controller, `GreetingController`,
40+
The following example demonstrates setting up
41+
`GreetingController`,
4142
which attaches a `greeting` property containing the string `'Hola!'` to the `$scope`:
4243

44+
We attach the controller as a module on the application using the `.controller` method of your
45+
{@link module Angular Module}. This keeps it out of the global scope.
46+
4347
```js
44-
function GreetingController($scope) {
48+
var myApp = angular.module('myApp',[]);
49+
50+
myApp.controller('GreetingController', ['$scope', function($scope) {
4551
$scope.greeting = 'Hola!';
46-
}
52+
}]);
4753
```
4854

55+
4956
Once the Controller has been attached to the DOM, the `greeting` property can be data-bound to the
5057
template:
5158

@@ -55,17 +62,7 @@ template:
5562
</div>
5663
```
5764

58-
**NOTE**: Although Angular allows you to create Controller functions in the global scope, this is
59-
not recommended. In a real application you should use the `.controller` method of your
60-
{@link module Angular Module} for your application as follows:
61-
62-
```js
63-
var myApp = angular.module('myApp',[]);
6465

65-
myApp.controller('GreetingController', ['$scope', function($scope) {
66-
$scope.greeting = 'Hola!';
67-
}]);
68-
```
6966

7067
We have used an **inline injection annotation** to explicitly specify the dependency
7168
of the Controller on the `$scope` service provided by Angular. See the guide on
@@ -333,6 +330,3 @@ describe('state', function() {
333330
});
334331
});
335332
```
336-
337-
338-

0 commit comments

Comments
 (0)