How can I use a window object in Angular 2?

Martin picture Martin · Feb 25, 2017 · Viewed 12.5k times · Source

I tried to put the following code to get a window object in angular 2:

      @Component({
          selector: 'app-slider',
          templateUrl: './slider.component.html',
          styleUrls: ['./slider.component.css'],
          providers: [
            SliderService, 
            { provide: "windowObject", useValue: window}
          ]
        })
        export class SliderComponent implements OnInit {
        
          sliderList: Slider[] = [];
        
          constructor( private _sliderservice:SliderService, @Inject("windowObject") private _window:window ) { }

Unfortunately it does not work.

Answer

Yaroslav Grishajev picture Yaroslav Grishajev · Feb 25, 2017

You don't need a provider for that. window is a global object and is accessible directly in your class