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

fix($controller): remove the option to instantiate controllers from c… #15762

Merged
merged 2 commits into from Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 9 additions & 25 deletions docs/content/tutorial/step_02.ngdoc
Expand Up @@ -150,38 +150,22 @@ To learn more about AngularJS scopes, see the {@link ng.$rootScope.Scope Angular

# Testing

The "AngularJS way" of separating controller from the view, makes it easy to test code as it is being
developed. If our controller were available on the global namespace, we could simply instantiate it
with a mock scope object:
## Testing Controllers

<br />
```js
describe('PhoneListController', function() {
The "AngularJS way" of separating the controller from the view makes it easy to test code as it is being
developed. In the section "Model and Controller" we have registered our controller via a constructor
function on the `phonecatApp` module.

it('should create a `phones` model with 3 phones', function() {
var scope = {};
var ctrl = new PhoneListController(scope);

expect(scope.phones.length).toBe(3);
});
In tests, we use an AngularJS service, `$controller`, which will retrieve a controller by name. It
also takes a second argument - a map of dependencies that should be injected.

});
```
The following test instantiates `PhoneListController` with a mock scope object,
and verifies that the phones array property on the scope contains three records.

The test instantiates `PhoneListController` and verifies that the phones array property on the
scope contains three records. This example demonstrates how easy it is to create a unit test for
This example demonstrates how easy it is to create a unit test for
code in AngularJS. Since testing is such a critical part of software development, we make it easy to
create tests in AngularJS so that developers are encouraged to write them.


## Testing non-global Controllers

In practice, you will not want to have your controller functions in the global namespace. Instead,
you can see that we have registered it via a constructor function on the `phonecatApp` module.

In this case AngularJS provides a service, `$controller`, which will retrieve your controller by name.
Here is the same test using `$controller`:

<br />
**`app/app.spec.js`:**

Expand Down
23 changes: 2 additions & 21 deletions src/ng/controller.js
Expand Up @@ -26,8 +26,7 @@ function identifierForController(controller, ident) {
* {@link ng.$controllerProvider#register register} method.
*/
function $ControllerProvider() {
var controllers = {},
globals = false;
var controllers = {};

/**
* @ngdoc method
Expand Down Expand Up @@ -55,21 +54,6 @@ function $ControllerProvider() {
}
};

/**
* @ngdoc method
* @name $controllerProvider#allowGlobals
* @description If called, allows `$controller` to find controller constructors on `window`
*
* @deprecated
* sinceVersion="v1.3.0"
* removeVersion="v1.7.0"
* This method of finding controllers has been deprecated.
*/
this.allowGlobals = function() {
globals = true;
};


this.$get = ['$injector', '$window', function($injector, $window) {

/**
Expand All @@ -83,8 +67,6 @@ function $ControllerProvider() {
*
* * check if a controller with given name is registered via `$controllerProvider`
* * check if evaluating the string on the current scope returns a constructor
* * if $controllerProvider#allowGlobals, check `window[constructor]` on the global
* `window` object (deprecated, not recommended)
*
* The string can use the `controller as property` syntax, where the controller instance is published
* as the specified property on the `scope`; the `scope` must be injected into `locals` param for this
Expand Down Expand Up @@ -124,8 +106,7 @@ function $ControllerProvider() {
identifier = identifier || match[3];
expression = controllers.hasOwnProperty(constructor)
? controllers[constructor]
: getter(locals.$scope, constructor, true) ||
(globals ? getter($window, constructor, true) : undefined);
: getter(locals.$scope, constructor, true);

if (!expression) {
throw $controllerMinErr('ctrlreg',
Expand Down
4 changes: 0 additions & 4 deletions src/ng/directive/ngController.js
Expand Up @@ -31,10 +31,6 @@
* The controller instance can be published into a scope property by specifying
* `ng-controller="as propertyName"`.
*
* If the current `$controllerProvider` is configured to use globals (via
* {@link ng.$controllerProvider#allowGlobals `$controllerProvider.allowGlobals()` }), this may
* also be the name of a globally accessible constructor function (deprecated, not recommended).
*
* @example
* Here is a simple form for editing user contact information. Adding, removing, clearing, and
* greeting are methods declared on the controller (see source tab). These methods can
Expand Down
2 changes: 0 additions & 2 deletions src/ngMock/angular-mocks.js
Expand Up @@ -2256,8 +2256,6 @@ angular.mock.$RootElementProvider = function() {
*
* * check if a controller with given name is registered via `$controllerProvider`
* * check if evaluating the string on the current scope returns a constructor
* * if $controllerProvider#allowGlobals, check `window[constructor]` on the global
* `window` object (deprecated, not recommended)
*
* The string can use the `controller as property` syntax, where the controller instance is published
* as the specified property on the `scope`; the `scope` must be injected into `locals` param for this
Expand Down
15 changes: 0 additions & 15 deletions test/ng/controllerSpec.js
Expand Up @@ -95,21 +95,6 @@ describe('$controller', function() {
});


it('should instantiate a controller defined on window if allowGlobals is set',
inject(function($window) {
var scope = {};
var Foo = function() {};

$controllerProvider.allowGlobals();

$window.a = {Foo: Foo};

var foo = $controller('a.Foo', {$scope: scope});
expect(foo).toBeDefined();
expect(foo instanceof Foo).toBe(true);
}));


it('should throw ctrlfmt if name contains spaces', function() {
expect(function() {
$controller('ctrl doom');
Expand Down