Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Smart assign to this in constructors #27

Open
ianldgs opened this issue Jul 28, 2017 · 4 comments
Open

Smart assign to this in constructors #27

ianldgs opened this issue Jul 28, 2017 · 4 comments

Comments

@ianldgs
Copy link

ianldgs commented Jul 28, 2017

angular.module('app').controller('Ctrl', class Ctrl {
  /* @ngInject */
  constructor($scope) {
    //some code
  }
});

could become:

angular.module('app').controller('Ctrl', class Ctrl {
  static $inject = ['$scope']
  constructor() {
    [this.$scope] = arguments;
    //some code
  }
});
@schmod
Copy link
Owner

schmod commented Jul 28, 2017

TypeScript-style automatic property assignment is probably going to remain outside of the scope of this package. It would create unexpected behaviors for a lot of users, and there are many edge-cases that would be difficult to implement.

However, you raise a good point that your first example does not work unless ES5 transformations are enabled, or Ctrl is declared outside of angular.module().

angular.module('app').controller('Ctrl', class Ctrl {
  /* @ngInject */
  constructor($scope) {
    //some code
  }
});

However, this does (currently) work:

class Ctrl {
  constructor($scope) {
    this.$scope = $scope;
  }

  dostuff() { this.foo='baz'; }
};

angular.module('app').controller('Ctrl', Ctrl);

I'll keep this open as a bug until that's fixed.

@ianldgs
Copy link
Author

ianldgs commented Jul 28, 2017

That is unfortunate. How about adding options to ngInject?

Something like:
/* @ngInject({autoAsign: true}) */

This is how annotations works in other languages.

About the issue of ngInject inside angular.module(), I didn't notice, as I use ES7 transformers, but I used it alot.-

@schmod
Copy link
Owner

schmod commented Jul 28, 2017

babel-plugin-auto-assign might do the thing that you're looking for.

It's not necessarily an Angular 1.x-specific feature.

@georgecrawford
Copy link

Hi @schmod. The issue you describe in the second part of #27 (comment) is exactly my problem. I was expecting this plugin to detect and convert a controller class defined inside angular.module(), but as you point out it does not.

Can you explain whether a fix is planned for this, and if not, what options I have as a workaround? What do you mean by unless ES5 transformations are enabled, for example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants