Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

this context inside directive functions are undefined, should be bound to definition object #8549

@pocesar

Description

@pocesar

In Typescript, you can define a directive like this (since angular.directive doesn't support constructors like controller, service, etc):

module stuff {
  export class MyDirective implements ng.IDirective {
     restrict = 'A';
     constructor(public injectSome: ngSome.injectSome) {}
     link(){
        this.injectSome(); // this is undefined
     }
     static instance(){
       return ['injectSome', (injectSome) => new this(injectSome)];
     }
  }
  angular.module('stuff', []).directive('myDirective', MyDirective.instance());
}

the link, compile, template and templateUrl functions should be called within the directive definition object context. this will make constructors work as they should.

Typescript enforces good practices and modularization, so you can even test stuff decoupled from angular itself (through the classes definitions), don't ruin the fun for us :)

(also happening on filters)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions