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

半城伤御伤魂 提交于 2019-12-10 04:46:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/51274406/upgrade-angularjs-1-5-to-1-6-which-exact-bindings-are-affected-by-change-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!