将AngularJs 1.5升级到1.6-$ compile reg控制器实例中的更改会影响哪些确切的绑定?

问题描述 投票:6回答:1

Documentation用于从AngularJs 1.5升级到1.6状态时$ compile的更改:

默认情况下禁用组件/指令控制器实例上的预分配绑定,这意味着它们将不再在构造函数中可用。

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

文档中的升级示例如下(缩短):

之前

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

之后

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

我已经发现我必须对使用bindToController:true的任何指令使用相同的$ onInit函数,如下所示:

.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 () { //...

是否有其他类型的绑定受此更改影响?

或者是否足以处理componentsbindToController:true的指令?

改写相同的问题:在Angular 1.7应用程序中,仅使用带有bindToController的指令:false:我可以完全面对有关预分配绑定的任何问题吗?

angularjs angularjs-directive angularjs-components angularjs-1.6
1个回答
0
投票

调用$ onInit()生命周期方法时,绑定完成。这是唯一的保证。假定值在构造函数中可用不再安全,这会影响整个应用程序。

我建议使用1.5样式的组件和ES6,以使将来的迁移更加容易。

© www.soinside.com 2019 - 2024. All rights reserved.