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.

DI doesn't work with ES6 classes unless the constructor is the first function of the class #14175

Closed
@mattlewis92

Description

@mattlewis92
  • Do you want to request a feature or report a bug?
    Bug
  • What is the current behaviour?
    If the constructor doesn't come first in an ES6 class (FYI I'm not using a transpiler, I'm using native ES6 in chrome 49) and there is no explicit annotation then the DI fails and every element is undefined in the constructor e.g.

This works

class test {
  constructor($rootScope) {
    console.log($rootScope);
  }

  test() {}
}
$injector.instantiate(test);

and this doesn't:

class test {

  test() {}

  constructor($rootScope) {
    console.log($rootScope); // undefined
  }
}
$injector.instantiate(test);

The DI to auto inject services

  • What is the motivation / use case for changing the behaviour?

The example I gave above is contrived, and normally I would just put the constructor first before any controller methods. However for resolves I often do something like this before a constructor, so you see resolves first and then the constructor where the results are injected:

class MyClass {

  static get resolve() {
    return {
      data: ($http) => $http.get('/path')
    };
  }

  constructor(data) {
    console.log(data);
  }

}
  • Which version of Angular, and which browser and OS does this issue affect? Did this work in previous
    versions of Angular? Please also test with the latest stable and snapshot versions.

1.5.0

  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix)

I think the fix should be simple enough by modifying the regex here and look for constructor( before the arguments.

If you need any more info let me know! 😄

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions