2

I write services and controllers in my app (Angularjs - 1.6.3) using es6 classes.

I want to simplify some piece of code but don't know whether it's possible and how to get es6 class constructor arguments list.

Example of piece i need to simplify to prevent duplication:

class MyService {

    constructor (
        $q,
        $translate,
    ) {

        this.$q = $q;
        this.$translate = $translate;
    }
}

angular
    .module('app')
    .service('myService', MyService);

Is there any way to add dependencies to given context automatically?

Know that in angular2 there are decorators which helps not to duplicate dependencies to add them in current scope (or they do this any other way)

1

1 Answer 1

0

You could use Object.assign combined with destructuring:

class Service {
    constructor($q, $translate) {
        Object.assign(this, { $q, $translate });
    }
}

This is equivalent to the code you wrote.

Sign up to request clarification or add additional context in comments.

3 Comments

If this is the answer, isn't the question a duplicate of this? I'm not sure what the OP meant by "get arguments list".
The question is indeed the same thing, formulated using other words.
@Husky it's the way i have it now, tx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.