AlreadyLost
AlreadyLost

Reputation: 827

Angular useValue and injection token

In my angular app I have

{ provide: QUEUE, useValue: window.events }

in my providers. I was wondering how can I set QUEUE to use an empty array [] if window.events are undefined. I tried something like this and it didn't work.

  { provide: QUEUE, useValue: window.events || [] }

Here is what I have in QUEUE.injection-token.ts

import { InjectionToken } from '@angular/core';

export const \QUEUE = new InjectionToken<Array<AppEvent>>('QUEUE');

Thanks!

Upvotes: 1

Views: 1125

Answers (1)

Estus Flask
Estus Flask

Reputation: 222780

window.events || [] check should be performed at runtime because window doesn't exist at compilation time.

It likely should be:

export function queueFactory(): any[] {
    return window.events || [];
}

...
providers: [ provide: QUEUE, useFactory: queueFactory }]
...

Upvotes: 2

Related Questions