angular-styleguide icon indicating copy to clipboard operation
angular-styleguide copied to clipboard

ES6 Class

Open darlanmendonca opened this issue 7 years ago • 4 comments

Any intention to use js classes es6 to controllers or services?

I'm looking a way to use these, i.e.

// instead
angular
  .module('app')
  .controller('LoginController', LoginController);

function LoginController() {
  this.credentials;
  this.authenticate = authenticate;

  function authenticate() {
    Authentication.login(this.credentials);
  }
}
// es6 implementation
class LoginController() {
  constructor(Authentication) {// injection dependency, I guess
    this.credentials = {};
  }

  authenticate() {
    Authentication.login()
  }
}

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

darlanmendonca avatar Sep 08 '16 13:09 darlanmendonca

It should go by

// es6 implementation

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

class LoginController() {
  constructor(Authentication) {// injection dependency, for sure.
    this.credentials = {};
    this.Authentication = Authentication;
  }

  authenticate() {
    this.Authentication.login()
  }
}

Or something... You can check angular-fullstack generator. I'm not spamming or something, I just used to used it, and they follow pretty good this guide. :).

michaeljota avatar Sep 12 '16 23:09 michaeljota

Yep, I know this generator, is good, unfortunately, they dont follow john papa styleguide.

One thing that bothers me, is the fact of class be generated on scope, only when interpreter read the line, different of functions, they be created in beggining of scope.

But class have better readability of methods and properties, without make ugly things :D, because of that, I'm considering if they be welcome in styleguide

darlanmendonca avatar Sep 13 '16 12:09 darlanmendonca

That follow this style guide as best as they willing to. I don't think class should be in any angular style guide, nor that any style guide should be followed strictly. I know I don't follow this strictly.

As you said, classes have better readbility, but Angular was not design arround that, so I think I doesn't make sense to style about it. It's my opinion though.

At the very end, this is just a style guide, the most popular one, yeah, but just a style guide. :).

Respects.

michaeljota avatar Sep 13 '16 12:09 michaeljota

I think you're looking for this fork.

GMaiolo avatar Dec 06 '16 17:12 GMaiolo