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

Commit aa3164c

Browse files
committed
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', { controller: 'MyController', template: '<div>{{ $ctrl.a }} + {{ $ctrl.b }} = {{ $ctrl.sum }}</div>', bindings: { a: '<', b: '<' } }) .controller('MyController', function() { this.sum = this.a + this.b; }); ``` After: ```js angular.module('myApp', []) .component('myComponent', { controller: 'MyController', template: '<div>{{ $ctrl.a }} + {{ $ctrl.b }} = {{ $ctrl.sum }}</div>', bindings: { a: '<', b: '<' } }) .controller('MyController', function() { this.$onInit = function () { this.sum = this.a + this.b; }; }); ``` If you need to support both Angular 1.4 and 1.6, e.g. you're writing a library, you need to check for Angular version and apply proper logic. Follow the example below: Before: ```js angular.module('myApp', []) .directive('myDirective', function() { return { controller: 'MyController', template: '<div>{{ $ctrl.a }} + {{ $ctrl.b }} = {{ $ctrl.sum }}</div>', scope: { a: '=', b: '=' }, bindToController: true, controllerAs: '$ctrl' }; }) .controller('MyController', function($scope) { this.sum = this.a + this.b; }); ``` After: ```js angular.module('myApp', []) .config(function($compileProvider) { $compileProvider.preAssignBindingsEnabled(false); }) .directive('myDirective', function() { return { controller: 'MyController', template: '<div>{{ $ctrl.a }} + {{ $ctrl.b }} = {{ $ctrl.sum }}</div>', scope: { a: '=', b: '=' }, bindToController: true, controllerAs: '$ctrl' }; }) .controller('MyController', function($scope) { this.$onInit = function() { this.sum = this.a + this.b; }; if (angular.version.major === 1 && angular.version.minor <= 4) { this.$onInit(); } }); ```
1 parent 856e300 commit aa3164c

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)