0

I have two TS repository classes

1) InvoiceRepository 2) SalesReceiptRepositroy

Here is how there constructor looks like

InvoiceRepository

export class InvoiceRepository extends RepositoryBase {

    constructor(
        protected http: Http,
        protected toasterService: ToasterService,
        protected progressbarService: ProgressBarService,
        invocieType: InvoiceType = InvoiceType.Invoice,
        @Inject('ApiEndpoint') protected apiEndpoint: string) {

        super(toasterService, progressbarService);
        console.log(invocieType);
    }
}

SalesReceiptRepositroy

export class SalesReceiptsRepository extends InvoiceRepository {

    constructor(http: Http,
        toasterService: ToasterService,
        progressbarService: ProgressBarService,
        @Inject('ApiEndpoint') apiEndpoint: string) {


        super(http, toasterService, progressbarService, InvoiceType.InvoiceCC, apiEndpoint);
    }
}

Questions:

1) I’m inheriting SalesReceiptRepository from InvoiceRepository, I have to pass all the required parameters to super class constructor. Would Angular separately inject dependency to Base class and Derived class when SalesReceiptRepoistory object get’s created? OR The base class will take from it’s derived class? Little confuse, please briefly explain how these two works together

2) If you look at the InvocieRepository constructor 3rd parameter, InvoiceType is an enum and I’m defaulting it to first value. But in the console statement, it’s logging value api/ which is actually the value of 4th parameter. So, what it’s doing, setting both 3rd and 4th parameter value to api/ where the 3rd one should be 1. This is only happening when InvoiceRepository object get’screated, however it behaves well when SalesReceipRepository object get’s created. Why?

8
  • 2) Why did you add this parameter in the first place? Did you provide provide(InvoiceType: {useValue: InvoiceType.SomeItem}) somewhere? Commented Jun 23, 2016 at 6:03
  • No, I want it to be set it to some default value when InvoiceRepository instance created by DI. However in case of SalesReceiptRepository, I manually calling the base class constructor so it appears to be working correctly. Commented Jun 23, 2016 at 10:17
  • I see. I guess you would need to add ? at the end (invocieType?: InvoiceType = ...)of the parameter name to mark it as optional. It might also be necessary to move optional parameters at the end of the parameter list (not sure about this). Commented Jun 23, 2016 at 11:13
  • I moved the optional param to last and tried the above but getting compiled time error Parameter can not have question mark and Initializer Commented Jun 23, 2016 at 11:22
  • Also tried by removing the question mark but then getting run time error No provider for Number Commented Jun 23, 2016 at 11:23

1 Answer 1

1

Angular will inject just in the derived class, so because of this you needed to repass every parameter to the super constructor. To better understand the situation I need to see where and how you provide this services, because the code appears absolutelly right.

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

Comments

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.