From faf7d95b59adddc987df7e19c77734a78cb17dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski?= Date: Wed, 2 Nov 2016 12:59:32 +0100 Subject: [PATCH] feat($compile): set preAssignBindingsEnabled to false by default Fixes #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. --- src/ng/compile.js | 2 +- test/ng/compileSpec.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 70727eab1b38..afd0a803b819 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1379,7 +1379,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { * * The default value is true in Angular 1.5.x but will switch to false in Angular 1.6.x. */ - var preAssignBindingsEnabled = true; + var preAssignBindingsEnabled = false; this.preAssignBindingsEnabled = function(enabled) { if (isDefined(enabled)) { preAssignBindingsEnabled = enabled; diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 3d2b31670196..b537977d2779 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -171,7 +171,9 @@ describe('$compile', function() { it('should allow preAssignBindingsEnabled to be configured', function() { module(function($compileProvider) { - expect($compileProvider.preAssignBindingsEnabled()).toBe(true); // the default + expect($compileProvider.preAssignBindingsEnabled()).toBe(false); // the default + $compileProvider.preAssignBindingsEnabled(true); + expect($compileProvider.preAssignBindingsEnabled()).toBe(true); $compileProvider.preAssignBindingsEnabled(false); expect($compileProvider.preAssignBindingsEnabled()).toBe(false); });