K
K
Konstantin2020-10-25 16:24:41
Angular
Konstantin, 2020-10-25 16:24:41

Why didn't the provider work?

Token:

export const SETTINGS = new InjectionToken<Settings>('SETTINGS');


Module - where the token resolves:

providers: [
        ExecutionFiltersRepository,
        ExecutionSortingsRepository,
         {
            provide: SETTINGS,
            useFactory: executionSettingsFactory,
            deps: [ExecutionFiltersRepository, ExecutionSortingsRepository, HttpClient],
        },
]


Factory:

const executionSettingsFactory = (
    filtersRepository: ExecutionFiltersRepository,
    sortingRepository: ExecutionSortingsRepository,
    http: HttpClient,
) => {
    return () => {
        alert(1);
        return new ExecutionSettings(filtersRepository, sortingRepository, http);
    };
};


The ExecutionSettings class itself:

@Injectable()
export class ExecutionSettings {
    constructor(
        private filtersRepository: ExecutionFiltersRepository,
        private sortingRepository: ExecutionSortingsRepository,
        private http: HttpClient,
    ) {    alert(2);}
}


Why is alert(1); not called? and alert(2);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LastDragon, 2020-10-26
@Junart1

Your factory `executionSettingsFactory` returns a closure, most likely it was meant:

const executionSettingsFactory = (
    filtersRepository: ExecutionFiltersRepository,
    sortingRepository: ExecutionSortingsRepository,
    http: HttpClient,
) => {
    alert(1);
    return new ExecutionSettings(filtersRepository, sortingRepository, http);
};

A
Anton Shvets, 2020-10-25
@Xuxicheta

there in the console is not written a thread like "arrow functions are not supported"?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question