【问题标题】:Why is bindToController deprecated in Typescript?为什么在 Typescript 中不推荐使用 bindToController?
【发布时间】:2016-12-15 13:16:53
【问题描述】:

当我使用 typescript 创建 angularjs 指令时,我使用属性 bindToController 将参数绑定到控制器,以便我可以访问它们

export class MyDirective implements IDirective {
    controller = MyController;
    controllerAs = 'vm';
    bindToController = {
        paramOne: '<',
        paramTwo: '<'
    };
}

export class MyController {
    paramOne: boolean; // theses params are now set and I can use them
    paramTwo: boolean;
    ...
}

但现在我意识到接口IDirective的参数bindToController已被弃用,尽管它仍然有效。

/**
 * @deprecated
 * Deprecation warning: although bindings for non-ES6 class controllers are currently bound to this before
 * the controller constructor is called, this use is now deprecated. Please place initialization code that
 * relies upon bindings inside a $onInit method on the controller, instead.
*/

消息应该解释它,但我还是不明白。

谁能解释为什么不推荐使用它以及最好的方法是什么?

【问题讨论】:

    标签: angularjs typescript binding


    【解决方案1】:

    bindToController 未被弃用。所描述的行为是。 TS界面的注释参考this part of the manual

    当前绑定的作用域属性在某些条件下已经在控制器构造函数中可用(绑定的父作用域属性此时应该在父控制器中可用)。

    此行为已弃用,绑定可能在控制器构造函数in future 中不可用。建议将所有依赖绑定的代码从构造函数移到$onInit钩子。

    默认行为在 1.6 中已更改,可以使用 $compileProvider.preAssignBindingsEnabled 更改。

    【讨论】:

      猜你喜欢
      • 2016-02-23
      • 2017-11-04
      • 2011-10-22
      • 2011-04-11
      • 2021-10-12
      • 2012-12-07
      • 2012-05-16
      • 2020-06-29
      • 2014-08-11
      相关资源
      最近更新 更多