iberbeu iberbeu - 1 year ago 143
TypeScript Question

Why is bindToController deprecated in Typescript?

When I create an angularjs Directive using typescript I use the property

to bind the parameters to the controller so that I can access them

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;

But now I realized that the parameter bindToController of the interface IDirective is deprecated, although it still works.

* @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.

The message should explain it but I still don't get it.

Can anyone explain why this is deprecated and what is the best way to proceed?

Answer Source

bindToController isn't deprecated. The described behaviour is. The comment in TS interface refers to this part of the manual.

Currently bound scope properties are already available in controller constructor on some conditions (bound parent scope properties should be available in parent controller at this moment).

This behaviour is deprecated, the bindings may not be available in controller constructor in future. It is suggested to move all code that relies on bindings from constructor to $onInit hook.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download