Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix($controller): remove the option to instantiate controllers from c…
…onstructors on `window`

This also removes the likewise deprecated `$controllerProvider.allowGlobals()` method.

Closes #15349

BREAKING CHANGE:

The option to instantiate controllers from constructors on the global `window` object
has been removed. Likewise, the deprecated `$controllerProvider.allowGlobals()`
method that could enable this behavior, has been removed.

This behavior had been deprecated since AngularJS v1.3.0, because polluting the global scope
is bad. To migrate, remove the call to $controllerProvider.allowGlobals() in the config, and
register your controller via the Module API or the $controllerProvider, e.g.

```
angular.module('myModule', []).controller('myController', function() {...});

angular.module('myModule', []).config(function($controllerProvider) {
  $controllerProvider.register('myController', function() {...});
});

```
  • Loading branch information
Narretz committed Feb 28, 2017
commit 678d5451c86b8d5390f7b200f287e2a4b61f0162
23 changes: 2 additions & 21 deletions src/ng/controller.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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