Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.0.0 received no aot compilation error on { provide: 'windowObject', useValue: window }, #15495

Closed
dereklin opened this issue Mar 26, 2017 · 4 comments
Labels
area: compiler Issues related to `ngc`, Angular's template compiler area: core Issues related to the framework runtime area: server Issues related to server-side rendering core: di freq2: medium type: bug/fix
Milestone

Comments

@dereklin
Copy link

When I was using 2.4.9 and tried to build aot, there was a compilation error regarding this:

{ provide: 'windowObject', useValue: window },

I upgraded to 4.0.0, and didn't see the error. I thought Angular 4 is capable of understanding this usage now. I was wrong. In production at runtime, the window object is undefined.

In the end, I used the approach described here: https://juristr.com/blog/2016/09/ng2-get-window-ref/ and it works in 4.0.0

But if Angular 4 can't inject the window object with { provide: 'windowObject', useValue: window }, it should give an error during compilation like it did in 2.4.9.

@DzmitryShylovich
Copy link
Contributor

Yeah, I can confirm that it doesn't throw during compilation but injects undefined.

@oleonardo
Copy link

oleonardo commented Jun 9, 2017

I use this solution: Universal "Gotchas"

import { PLATFORM_ID, Component, Inject, OnInit } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { JQUERY_TOKEN } from './../../services/common/index';

@Component({
    selector: 'home',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
    constructor(
        @Inject(PLATFORM_ID) private platformId: Object,
        @Inject(JQUERY_TOKEN) private $: JQueryStatic
    ) {
    }

    ngOnInit() {
        //workaround to avoid break on server side prerendering
        if (!isPlatformBrowser(this.platformId)) return;

        let $window = this.$(window);
        console.log($window);
    }
}

It works well for me, I hope this helps you.

@ngbot ngbot bot added this to the Backlog milestone Feb 6, 2018
@IgorMinar IgorMinar removed the area: core Issues related to the framework runtime label Nov 27, 2018
@IgorMinar IgorMinar added area: core Issues related to the framework runtime and removed area: core Issues related to the framework runtime comp: metadata-extractor labels Nov 27, 2018
@pkozlowski-opensource pkozlowski-opensource added core: di area: compiler Issues related to `ngc`, Angular's template compiler area: server Issues related to server-side rendering labels Mar 12, 2020
@JoostK
Copy link
Member

JoostK commented Mar 15, 2020

I can no longer reproduce in Angular 9, neither using ViewEngine nor Ivy. Closing as resolved.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: compiler Issues related to `ngc`, Angular's template compiler area: core Issues related to the framework runtime area: server Issues related to server-side rendering core: di freq2: medium type: bug/fix
Projects
None yet
Development

No branches or pull requests

8 participants