Search code examples
angularangular2-cliwindow-object

How can I use a window object in Angular 2?


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.


Solution

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