Skip to content

Can not inject dependencies on class when declared on an object property #56

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
g0di opened this issue Jul 2, 2020 · 0 comments
Open

Comments

@g0di
Copy link

g0di commented Jul 2, 2020

Hello there,

I'm stuck with an issue with following angularJS component declaration where the plugin does not generate $inject array

export const MyComponent = {
  template: '...',
  bindings: {},
  controller: class MyComponentController {
    constructor ($http) {
      'ngInject'
    }
  }
}

I think this is because the plugin has nowhere to include the MyComponentController.$inject = ['$http'] statement because of the way the class is declared and associated to the controller property.

There is two possible solutions in this case

  1. Add a MyComponent.controller.$inject = [...] statement
  2. (my favorite) Add a class property to end up with something like:
export const MyComponent = {
  template: '...',
  bindings: {},
  controller: class MyComponentController {
    constructor ($http) {
      'ngInject'
    }
    get $inject() {
        return ['$http']
    }
  }
}

For the moment I use the following workaround:

export const MyComponent = {
  template: '...',
  bindings: {},
  controller: (() => {
    class MyComponentController {
      constructor ($http) {
        'ngInject'
      }
    }
    return MyComponentController
  })()
}

It is a bit more verbose and add an additional nested level to the code

What do you think of this?

Thank you for reading !

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant