1

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!

AlreadyLost
  • 767
  • 2
  • 13
  • 28
  • Please, update the question with what *it didn't work* means. If there's error message, cite it. This will help other users. – Estus Flask Oct 20 '18 at 07:30

1 Answers1

2

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 }]
...
Estus Flask
  • 206,104
  • 70
  • 425
  • 565