将 AngularJs 1.5 升级到 1.6 - $compile reg 控制器实例的变化会影响哪些确切的绑定? [英] Upgrade AngularJs 1.5 to 1.6 - which exact bindings are affected by change in $compile reg controller instances?

查看:17
本文介绍了将 AngularJs 1.5 升级到 1.6 - $compile reg 控制器实例的变化会影响哪些确切的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档$compile 从 AngularJs 1.5 升级到 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 开发人员指南 - 迁移到 V1.6 - $编译

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

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

之前

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

之后

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

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

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?

或者用bindToController:true处理组件指令就足够了吗?

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

重新表述相同的问题:在 Angular 1.7 应用程序中,仅使用 指令和 bindToController: false:我是否会遇到任何关于预分配绑定的问题?

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?

推荐答案

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

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.

我建议改用 1.5 样式的组件和 ES6,以便将来更轻松地进行迁移.

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

这篇关于将 AngularJs 1.5 升级到 1.6 - $compile reg 控制器实例的变化会影响哪些确切的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆