Upgrade AngularJs 1.5 to 1.6 - which exact bindings are affected by change in $compile reg controller instances?
Asked Answered
P

1

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?

Packet answered 10/7, 2018 at 21:53 Comment(4)
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?.Costotomy
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.Costotomy
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.Costotomy
Thanks, appreciated. But we started application long ago and will stay on 1.x during LTS support.Packet
A
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.

Allopathy answered 4/11, 2019 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.