K
K
Konstantin2020-09-26 20:57:12
Angular
Konstantin, 2020-09-26 20:57:12

How to create DI as a factory?

I have two custom classes:

export class DocumentBlock extends DocumentCustomBlock {}
export class DepartamentBlock extends DocumentCustomBlock {}


Model:

class CustomBlockModel {
   constructor(public type: string, id: number)
}


And there is a factory function:
export const documentCustomBlockFactory = (httpClient: HttpClient, model: CustomBlockModel) => {
    try {
        switch (model.type) {
            case 'doc_num_date_block':
                return new DocNumDateBlock(httpClient, model);
            case 'dep_block':
                return new DepartamentBlock(httpClient, model);

            default:
                throw new Error('Undefined block type!');
        }
    } catch (e) {
        console.log('ERROR: ' + e);
    }
};


I need to create a concrete class and pass it as parameters to MatDialog.

Inside the component, DocumentListI get the response data as an array:

this.documents = response;

Then I get one element of the array and I need to create an instance:

let model = new CustomBlockModel(this.documents[0]);
matDialog.config.data = documentCustomBlockFactory(this.httpClient, model);


How to make it so with Angular providers and factory provider?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question