K
K
Konstantin2020-09-29 15:55:53
Angular
Konstantin, 2020-09-29 15:55:53

Why is the data not updated in the service?

I have a module with a factory defined:

providers: [
        CustomBlockService,
        {
            provide: DocumentCustomBlock,
            useFactory: (httpClient: HttpClient, customBlockService: CustomBlockService) => {
                switch (customBlockService.block.type) {
                    case 'doc_num_date_block':
                        return new DocNumDateBlock(
                            httpClient,
                            customBlockService,
                            new DocNumDateBlockFormGroup(createFormControls(customBlockService.value)),
                        );
                    case 'find_unom_by_address_block':
                        return new FindUnomAddressBlock(
                            httpClient,
                            customBlockService,
                            new FindUnomAddressBlockFormGroup(createFormControls(customBlockService.value)),
                        );
                    default:
                        throw 'Error type custom block!';
                }
            },
            deps: [HttpClient, CustomBlockService],
        },
    ],


This dialog box module is pulled from the parent component, before opening the window, the selected block is entered into the CustomBlockService service:

public setDocument(): void {
        this.customBlockService.block = this.block;
}


The first time it works, in the dialog I see the selected data this.customBlockService.block = this.block;in provider D ocumentCustomBlock.

When the window closes and reopens with a new this.customBlockService.block = this.block; one, I see the old data in the service.

Why is the service not updated in this case?

I partially understood the reason - the service resolves once and is cached, the question is ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2020-09-29
@Junart1

The factory is created once when the dependency injection mechanisms are called .
What you are trying to implement is called a factory provider .
You correctly understood the reason - the result of the execution is cached due to the fact that the instantiated service is cached.
You need to remove block instantiation inside the service.
Something like that:

providers: [
    CustomBlockService,
    {
        provide: DocumentCustomBlock,
        useFactory: (httpClient: HttpClient, customBlockService: CustomBlockService) => {
            return new ControlBlock(httpClient, customBlockService.block.type);
        },
        deps: [HttpClient, CustomBlockService],
    },
],

But trust me, you're overcomplicating things. I advise you to break this case into components and simply load the one you need into the dialog box. This will save you some brainwashing during implementation and will make it easier to maintain in the future.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question