6

Documentation for a change in $compile when upgrading from AngularJs 1.5 to 1.6 states:

pre-assigning bindings on component/directive controller instances is disabled by default, which means that they will no longer be available inside the constructors.

— AngularJS Developer Guide - Migrating to V1.6 - $compile

The upgrade example in the documentation is as follows (shortened):

Before

.component('myComponent', {
  bindings: {value: '<'},
  controller: function() {
    //...
  }
})

After

.component('myComponent', {
  bindings: {value: '<'},
  controller: function() {
    this.$onInit = function() {
      // ...
    };
  }
})

I already discovered that I have to use the same $onInit function for any directive using bindToController: true like here:

.directive('acAllocation', acAllocation);

  function acAllocation(SomeService) {
    return {
      restrict: 'E',
      replace: true,
      scope: {
        allocation: '=acAllocation'
      },
      controller: acAllocationController,
      controllerAs: 'vm',
      bindToController: true,
      templateUrl: 'path/acAllocation.html'
    };

    function acAllocationController() {

      var vm = this;

      this.$onInit = function () { //...

Are there any other types of bindings which are affected by this change?

Or is it enough to deal with components and directives with bindToController:true?

Rephrasing the same question: In an Angular 1.7 application only using directives with bindToController: false: can I face any issues regarding pre-assigning bindings at all?

Community
  • 1
  • 1
LBA
  • 3,859
  • 2
  • 21
  • 60
  • Be aware that the AngularJS team recommends the `replace: true` property be avoided. For more information, see [Why is `replace` property deprecated in AngularJS directives?](https://stackoverflow.com/a/35545445/5535245). – georgeawg Jul 10 '18 at 22:56
  • Going forward bi-directional binding with `=` should be avoided. It makes the migration to Angular 2+ difficult. For more information, see [AngularJS Developer Guide - Component-based application architecture](https://docs.angularjs.org/guide/component#component-based-application-architecture). – georgeawg Jul 10 '18 at 22:59
  • 1
    Short answer is that it affects all isolate scope bindings whether they are bound to either the scope or the controller. I will write a more in depth answer later. – georgeawg Jul 10 '18 at 23:19
  • Thanks, appreciated. But we started application long ago and will stay on 1.x during LTS support. – LBA Jul 10 '18 at 23:21

1 Answers1

0

The bindings are complete when $onInit() lifecycle method is called. This is the only guarantee. It's no longer safe to assume that values are available in the constructor, and this affects the entire application.

I would recommend moving to 1.5 style components and ES6 in order to make migration easier in the future.

Stefan Norberg
  • 1,137
  • 13
  • 28