ngUpgraders / ng-forward

The default solution for those that want to write Angular 2.x style code in Angular 1.x
411 stars 36 forks source link

Inject decorator that sets `this.$http` when injecting $http? #87

Closed Rush closed 8 years ago

Rush commented 8 years ago

Thanks for this awesome project, I just started using it and I have a question, right now I have to do:

@Injectable()
@Inject('$http')
export class billingService {
  constructor($http) {
    this.$http = $http;
  }
  get() {
    return this.$http.get('/api/billing.json');
  }
}

I don't like the redundancy of injection params though, is there a way to omit writing the below altogether?

  constructor($http) {
    this.$http = $http;
  }

This could be set automatically by the decorator.

pbastowski commented 8 years ago

Usung TypeScript the code can be refactored to

  constructor(private $http) {
  }

which will automatically put $http on this

Rush commented 8 years ago

Thanks, good to know but I'd like to know about ES6.

timkindberg commented 8 years ago

@rush not a bad idea. I think it sounds good.

Rush commented 8 years ago

Any ideas how could it be implemented then? (I am pretty new to decorators and all this ES6/7/TS stuff)

timkindberg commented 8 years ago

We would add them in directive-controller.js. That's where we instantiate the components controller. We have access to the injects there so we'd just have to get their references from the injector and add them as properties. Just like we do with inputs kind of. On Wed, Nov 18, 2015 at 3:59 PM Damian Kaczmarek notifications@github.com wrote:

Any ideas how could it be implemented then? (I am pretty new to decorators and all this ES6/7/TS stuff)

— Reply to this email directly or view it on GitHub https://github.com/ngUpgraders/ng-forward/issues/87#issuecomment-157861686 .

  • Tim
timkindberg commented 8 years ago

After discussing with @MikeRyan52 we aren't going to add this feature. While it's a good idea, it's not a feature of Angular 2 and we are trying to keep parity with the ng2 feature set since this is essentially a polyfill library. As @pbastowski stated you can get this feature from TypeScript, which is how you'd have to do it in Angular 2 as well. Thanks and sorry!