Permalink
Browse files

feat($compile): set preAssignBindingsEnabled to false by default

Fixes #15350
Closes #15352

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.
  • Loading branch information...
mgol committed Nov 2, 2016
1 parent 0ff10e1 commit bcd0d4d896d0dfdd988ff4f849c1d40366125858
Showing with 4 additions and 2 deletions.
  1. +1 −1 src/ng/compile.js
  2. +3 −1 test/ng/compileSpec.js
View
@@ -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;
View
@@ -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);
});

3 comments on commit bcd0d4d

@mgol

This comment has been minimized.

Show comment
Hide comment
@mgol

mgol Nov 4, 2016

Member

There's a mistake in the last example; it should say:

angular.module('myApp', [])
  .config(function($compileProvider) {
    $compileProvider.preAssignBindingsEnabled(true);
  })
  .component('myComponent', {
    bindings: {value: '<'},
    controller: function() {
      this.doubleValue = this.value * 2;
    }
  });

The third line incorrectly said:

$compileProvider.preAssignBindingsEnabled(false);

instead of the correct:

$compileProvider.preAssignBindingsEnabled(true);
Member

mgol replied Nov 4, 2016

There's a mistake in the last example; it should say:

angular.module('myApp', [])
  .config(function($compileProvider) {
    $compileProvider.preAssignBindingsEnabled(true);
  })
  .component('myComponent', {
    bindings: {value: '<'},
    controller: function() {
      this.doubleValue = this.value * 2;
    }
  });

The third line incorrectly said:

$compileProvider.preAssignBindingsEnabled(false);

instead of the correct:

$compileProvider.preAssignBindingsEnabled(true);
@georgeawg

This comment has been minimized.

Show comment
Hide comment
@georgeawg

georgeawg Mar 18, 2017

The Deprecation Warning for $compile bindToController needs to be changed to a Deprecation Notice.
See https://docs.angularjs.org/api/ng/service/$compile#-bindtocontroller-

georgeawg replied Mar 18, 2017

The Deprecation Warning for $compile bindToController needs to be changed to a Deprecation Notice.
See https://docs.angularjs.org/api/ng/service/$compile#-bindtocontroller-

@mgol

This comment has been minimized.

Show comment
Hide comment
@mgol

mgol Mar 29, 2017

Member

@georgeawg I think it can be removed in 1.7 where we never pre-assign bindings but change the notice in 1.6. PR for 1.7 is #15870 and I committed a change to 1.6 in 77fad09.

Member

mgol replied Mar 29, 2017

@georgeawg I think it can be removed in 1.7 where we never pre-assign bindings but change the notice in 1.6. PR for 1.7 is #15870 and I committed a change to 1.6 in 77fad09.

Please sign in to comment.