ngAutocomplete icon indicating copy to clipboard operation
ngAutocomplete copied to clipboard

"document is not defined" when running on Angular Universal

Open treble-maker123 opened this issue 7 years ago • 3 comments

ERROR ReferenceError: document is not defined at Ng2AutoCompleteDirective.ngOnInit (xxxx) at checkAndUpdateDirectiveInline (xxxx) at checkAndUpdateNodeInline (xxxx) at checkAndUpdateNode (xxxx) at prodCheckAndUpdateNode (xxxx) at Object.r.ɵvid.e [as updateDirectives] (xxxx) at Object.updateDirectives (xxxx) at checkAndUpdateView (xxxx)

The compiled JS code looks like this,

Ng2AutoCompleteDirective.prototype.ngOnInit = function () {
        // wrap this element with <div class="ng2-auto-complete">
        This line ==> this.wrapperEl = document.createElement("div");
        this.wrapperEl.className = "ng2-auto-complete-wrapper";
        this.wrapperEl.style.position = "relative";
        this.el.parentElement.insertBefore(this.wrapperEl, this.el.nextSibling);
......

This isn't breaking anything, just throwing errors on the server side.

treble-maker123 avatar Dec 10 '17 03:12 treble-maker123

I have same issue how you solve this?

keshari05 avatar Jan 25 '19 12:01 keshari05

It's been a long time, and I can't find what I did. But one idea is that you can probably wrap the component using ngAutoComplete in a server-check, e.g. if running on the component, don't render the component. This might not work for all scenarios though.

treble-maker123 avatar Jan 25 '19 18:01 treble-maker123

I will look at this tomorrow. I think that its possible to refactor the component not to use document.

Are you at the latest version?

sengirab avatar Jan 25 '19 20:01 sengirab