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

Commit 38f8c97

Browse files
committedMar 15, 2017
fix($compile): remove the preAssignBindingsEnabled flag
Closes #15782 BREAKING CHANGE: Previously, the `$compileProvider.preAssignBindingsEnabled` flag was supported. The flag controlled whether bindings were available inside the controller constructor or only in the `$onInit` hook. The bindings are now no longer available in the constructor. To migrate your code: 1. If you haven't invoked `$compileProvider.preAssignBindingsEnabled()` you don't have to do anything to migrate. 2. If you specified `$compileProvider.preAssignBindingsEnabled(false)`, you can remove that statement - since AngularJS 1.6.0 this is the default so your app should still work even in AngularJS 1.6 after such removal. Afterwards, migrating to AngularJS 1.7.0 shouldn't require any further action. 3. If you specified `$compileProvider.preAssignBindingsEnabled(true)` you need to first migrate your code so that the flag can be flipped to `false`. The instructions on how to do that are available in the "Migrating from 1.5 to 1.6" guide: https://docs.angularjs.org/guide/migration#migrating-from-1-5-to-1-6 Afterwards, remove the `$compileProvider.preAssignBindingsEnabled(true)` statement.
1 parent c80fa1c commit 38f8c97

File tree

4 files changed

+5467
-5739
lines changed

4 files changed

+5467
-5739
lines changed
 

‎src/ng/compile.js

+4-56
Original file line numberDiff line numberDiff line change
@@ -1372,36 +1372,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13721372
return debugInfoEnabled;
13731373
};
13741374

1375-
/**
1376-
* @ngdoc method
1377-
* @name $compileProvider#preAssignBindingsEnabled
1378-
*
1379-
* @param {boolean=} enabled update the preAssignBindingsEnabled state if provided, otherwise just return the
1380-
* current preAssignBindingsEnabled state
1381-
* @returns {*} current value if used as getter or itself (chaining) if used as setter
1382-
*
1383-
* @kind function
1384-
*
1385-
* @description
1386-
* Call this method to enable/disable whether directive controllers are assigned bindings before
1387-
* calling the controller's constructor.
1388-
* If enabled (true), the compiler assigns the value of each of the bindings to the
1389-
* properties of the controller object before the constructor of this object is called.
1390-
*
1391-
* If disabled (false), the compiler calls the constructor first before assigning bindings.
1392-
*
1393-
* The default value is true in AngularJS 1.5.x but will switch to false in AngularJS 1.6.x.
1394-
*/
1395-
var preAssignBindingsEnabled = false;
1396-
this.preAssignBindingsEnabled = function(enabled) {
1397-
if (isDefined(enabled)) {
1398-
preAssignBindingsEnabled = enabled;
1399-
return this;
1400-
}
1401-
return preAssignBindingsEnabled;
1402-
};
1403-
1404-
14051375
var TTL = 10;
14061376
/**
14071377
* @ngdoc method
@@ -2722,33 +2692,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
27222692
var controller = elementControllers[name];
27232693
var bindings = controllerDirective.$$bindings.bindToController;
27242694

2725-
if (preAssignBindingsEnabled) {
2726-
if (bindings) {
2727-
controller.bindingInfo =
2728-
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
2729-
} else {
2730-
controller.bindingInfo = {};
2731-
}
2732-
2733-
var controllerResult = controller();
2734-
if (controllerResult !== controller.instance) {
2735-
// If the controller constructor has a return value, overwrite the instance
2736-
// from setupControllers
2737-
controller.instance = controllerResult;
2738-
$element.data('$' + controllerDirective.name + 'Controller', controllerResult);
2739-
if (controller.bindingInfo.removeWatches) {
2740-
controller.bindingInfo.removeWatches();
2741-
}
2742-
controller.bindingInfo =
2743-
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
2744-
}
2745-
} else {
2746-
controller.instance = controller();
2747-
$element.data('$' + controllerDirective.name + 'Controller', controller.instance);
2748-
controller.bindingInfo =
2749-
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
2695+
controller.instance = controller();
2696+
$element.data('$' + controllerDirective.name + 'Controller', controller.instance);
2697+
controller.bindingInfo =
2698+
initializeDirectiveBindings(controllerScope, attrs, controller.instance, bindings, controllerDirective);
27502699
}
2751-
}
27522700

27532701
// Bind the required controllers to the controller, if `require` is an object and `bindToController` is truthy
27542702
forEach(controllerDirectives, function(controllerDirective, name) {

‎src/ngMock/angular-mocks.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -2207,11 +2207,6 @@ angular.mock.$RootElementProvider = function() {
22072207
* A decorator for {@link ng.$controller} with additional `bindings` parameter, useful when testing
22082208
* controllers of directives that use {@link $compile#-bindtocontroller- `bindToController`}.
22092209
*
2210-
* Depending on the value of
2211-
* {@link ng.$compileProvider#preAssignBindingsEnabled `preAssignBindingsEnabled()`}, the properties
2212-
* will be bound before or after invoking the constructor.
2213-
*
2214-
*
22152210
* ## Example
22162211
*
22172212
* ```js
@@ -2267,22 +2262,13 @@ angular.mock.$RootElementProvider = function() {
22672262
* the `bindToController` feature and simplify certain kinds of tests.
22682263
* @return {Object} Instance of given controller.
22692264
*/
2270-
function createControllerDecorator(compileProvider) {
2265+
function createControllerDecorator() {
22712266
angular.mock.$ControllerDecorator = ['$delegate', function($delegate) {
22722267
return function(expression, locals, later, ident) {
22732268
if (later && typeof later === 'object') {
2274-
var preAssignBindingsEnabled = compileProvider.preAssignBindingsEnabled();
2275-
22762269
var instantiate = $delegate(expression, locals, true, ident);
2277-
if (preAssignBindingsEnabled) {
2278-
angular.extend(instantiate.instance, later);
2279-
}
2280-
22812270
var instance = instantiate();
2282-
if (!preAssignBindingsEnabled || instance !== instantiate.instance) {
2283-
angular.extend(instance, later);
2284-
}
2285-
2271+
angular.extend(instance, later);
22862272
return instance;
22872273
}
22882274
return $delegate(expression, locals, later, ident);

‎test/ng/compileSpec.js

+5,431-5,554
Large diffs are not rendered by default.

‎test/ngMock/angular-mocksSpec.js

+30-113
Original file line numberDiff line numberDiff line change
@@ -2039,14 +2039,29 @@ describe('ngMock', function() {
20392039

20402040
describe('$controllerDecorator', function() {
20412041

2042-
describe('with `preAssignBindingsEnabled(true)`', function() {
2043-
2044-
beforeEach(module(function($compileProvider) {
2045-
$compileProvider.preAssignBindingsEnabled(true);
2046-
}));
2042+
it('should support creating controller with bindings', function() {
2043+
var called = false;
2044+
var data = [
2045+
{ name: 'derp1', id: 0 },
2046+
{ name: 'testname', id: 1 },
2047+
{ name: 'flurp', id: 2 }
2048+
];
2049+
module(function($controllerProvider) {
2050+
$controllerProvider.register('testCtrl', function() {
2051+
expect(this.data).toBeUndefined();
2052+
called = true;
2053+
});
2054+
});
2055+
inject(function($controller, $rootScope) {
2056+
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
2057+
expect(ctrl.data).toBe(data);
2058+
expect(called).toBe(true);
2059+
});
2060+
});
20472061

20482062

2049-
it('should support creating controller with bindings', function() {
2063+
it('should support assigning bindings when a value is returned from the constructor',
2064+
function() {
20502065
var called = false;
20512066
var data = [
20522067
{ name: 'derp1', id: 0 },
@@ -2055,138 +2070,40 @@ describe('ngMock', function() {
20552070
];
20562071
module(function($controllerProvider) {
20572072
$controllerProvider.register('testCtrl', function() {
2058-
expect(this.data).toBe(data);
2073+
expect(this.data).toBeUndefined();
20592074
called = true;
2075+
return {};
20602076
});
20612077
});
20622078
inject(function($controller, $rootScope) {
20632079
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
20642080
expect(ctrl.data).toBe(data);
20652081
expect(called).toBe(true);
20662082
});
2067-
});
2068-
2069-
2070-
it('should support assigning bindings when a value is returned from the constructor',
2071-
function() {
2072-
var called = false;
2073-
var data = [
2074-
{ name: 'derp1', id: 0 },
2075-
{ name: 'testname', id: 1 },
2076-
{ name: 'flurp', id: 2 }
2077-
];
2078-
module(function($controllerProvider) {
2079-
$controllerProvider.register('testCtrl', function() {
2080-
expect(this.data).toBe(data);
2081-
called = true;
2082-
return {};
2083-
});
2084-
});
2085-
inject(function($controller, $rootScope) {
2086-
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
2087-
expect(ctrl.data).toBe(data);
2088-
expect(called).toBe(true);
2089-
});
2090-
}
2091-
);
2092-
2093-
2094-
if (/chrome/.test(window.navigator.userAgent)) {
2095-
it('should support assigning bindings to class-based controller', function() {
2096-
var called = false;
2097-
var data = [
2098-
{ name: 'derp1', id: 0 },
2099-
{ name: 'testname', id: 1 },
2100-
{ name: 'flurp', id: 2 }
2101-
];
2102-
module(function($controllerProvider) {
2103-
// eslint-disable-next-line no-eval
2104-
var TestCtrl = eval('(class { constructor() { called = true; } })');
2105-
$controllerProvider.register('testCtrl', TestCtrl);
2106-
});
2107-
inject(function($controller, $rootScope) {
2108-
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
2109-
expect(ctrl.data).toBe(data);
2110-
expect(called).toBe(true);
2111-
});
2112-
});
21132083
}
2114-
});
2115-
2084+
);
21162085

2117-
describe('with `preAssignBindingsEnabled(false)`', function() {
21182086

2119-
beforeEach(module(function($compileProvider) {
2120-
$compileProvider.preAssignBindingsEnabled(false);
2121-
}));
2122-
2123-
2124-
it('should support creating controller with bindings', function() {
2087+
if (/chrome/.test(window.navigator.userAgent)) {
2088+
it('should support assigning bindings to class-based controller', function() {
21252089
var called = false;
21262090
var data = [
21272091
{ name: 'derp1', id: 0 },
21282092
{ name: 'testname', id: 1 },
21292093
{ name: 'flurp', id: 2 }
21302094
];
21312095
module(function($controllerProvider) {
2132-
$controllerProvider.register('testCtrl', function() {
2133-
expect(this.data).toBeUndefined();
2134-
called = true;
2135-
});
2096+
// eslint-disable-next-line no-eval
2097+
var TestCtrl = eval('(class { constructor() { called = true; } })');
2098+
$controllerProvider.register('testCtrl', TestCtrl);
21362099
});
21372100
inject(function($controller, $rootScope) {
21382101
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
21392102
expect(ctrl.data).toBe(data);
21402103
expect(called).toBe(true);
21412104
});
21422105
});
2143-
2144-
2145-
it('should support assigning bindings when a value is returned from the constructor',
2146-
function() {
2147-
var called = false;
2148-
var data = [
2149-
{ name: 'derp1', id: 0 },
2150-
{ name: 'testname', id: 1 },
2151-
{ name: 'flurp', id: 2 }
2152-
];
2153-
module(function($controllerProvider) {
2154-
$controllerProvider.register('testCtrl', function() {
2155-
expect(this.data).toBeUndefined();
2156-
called = true;
2157-
return {};
2158-
});
2159-
});
2160-
inject(function($controller, $rootScope) {
2161-
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
2162-
expect(ctrl.data).toBe(data);
2163-
expect(called).toBe(true);
2164-
});
2165-
}
2166-
);
2167-
2168-
2169-
if (/chrome/.test(window.navigator.userAgent)) {
2170-
it('should support assigning bindings to class-based controller', function() {
2171-
var called = false;
2172-
var data = [
2173-
{ name: 'derp1', id: 0 },
2174-
{ name: 'testname', id: 1 },
2175-
{ name: 'flurp', id: 2 }
2176-
];
2177-
module(function($controllerProvider) {
2178-
// eslint-disable-next-line no-eval
2179-
var TestCtrl = eval('(class { constructor() { called = true; } })');
2180-
$controllerProvider.register('testCtrl', TestCtrl);
2181-
});
2182-
inject(function($controller, $rootScope) {
2183-
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
2184-
expect(ctrl.data).toBe(data);
2185-
expect(called).toBe(true);
2186-
});
2187-
});
2188-
}
2189-
});
2106+
}
21902107
});
21912108

21922109

0 commit comments

Comments
 (0)
This repository has been archived.