Skip to content

Commit faf7d95

Browse files
committed
feat($compile): set preAssignBindingsEnabled to false by default
Fixes angular#15350 BREAKING CHANGE: Previously, $compileProvider.preAssignBindingsEnabled was set to true by default. This means bindings were pre-assigned in component constructors. In Angular 1.5+ the place to put the initialization logic relying on bindings being present is the controller $onInit method. To migrate follow the example below: Before: ```js angular.module('myApp', []) .component('myComponent', { bindings: {value: '<'}, controller: function() { this.doubleValue = this.value * 2; } }); ``` After: ```js angular.module('myApp', []) .component('myComponent', { bindings: {value: '<'}, controller: function() { this.$onInit = function() { this.doubleValue = this.value * 2; }; } }); ``` If you don't have time to migrate the code at the moment, you can flip the setting back to true: ```js angular.module('myApp', []) .config(function($compileProvider) { $compileProvider.preAssignBindingsEnabled(false); }) .component('myComponent', { bindings: {value: '<'}, controller: function() { this.doubleValue = this.value * 2; } }); ``` Don't do this if you're writing a library, though, as you shouldn't change global configuration then.
1 parent 0ff10e1 commit faf7d95

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13791379
*
13801380
* The default value is true in Angular 1.5.x but will switch to false in Angular 1.6.x.
13811381
*/
1382-
var preAssignBindingsEnabled = true;
1382+
var preAssignBindingsEnabled = false;
13831383
this.preAssignBindingsEnabled = function(enabled) {
13841384
if (isDefined(enabled)) {
13851385
preAssignBindingsEnabled = enabled;

test/ng/compileSpec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ describe('$compile', function() {
171171

172172
it('should allow preAssignBindingsEnabled to be configured', function() {
173173
module(function($compileProvider) {
174-
expect($compileProvider.preAssignBindingsEnabled()).toBe(true); // the default
174+
expect($compileProvider.preAssignBindingsEnabled()).toBe(false); // the default
175+
$compileProvider.preAssignBindingsEnabled(true);
176+
expect($compileProvider.preAssignBindingsEnabled()).toBe(true);
175177
$compileProvider.preAssignBindingsEnabled(false);
176178
expect($compileProvider.preAssignBindingsEnabled()).toBe(false);
177179
});

0 commit comments

Comments
 (0)