question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular Universal: '.setElementClass' failing when being called with 'undefined' as className

See original GitHub issue

The two icon directives mz-icon and mz-icon-mdi both call the .setElementClass function of the Renderer with a className value of undefined. This will not cause any issues in the browser as raising an error would be against the spec, but due to Angular Universal pre-rendering HTML on the server using the domino package, it causes things to break.

Current Behavior

Attempting to pre-render an Angular app on the server throws an error when trying to add / remove a class that is undefined, despite this being within the spec. This is most likely due to Angular using the domino package to create the DOM in NodeJS that doesn’t allow the input of undefined as className while adding / removing a CSS class to an element:

Error handler that throws the error (Source)

function handleErrors(token) {
  if (token === "" || token === undefined) {
    utils.SyntaxError();
  }
  ...
}

There are a three places where this is causing issues:

Possible Solution

Current:

handleAlign(previousValue?: string) {
  if (previousValue) {
    this.renderer.setElementClass(this.elementRef.nativeElement, previousValue, false);
  }
  this.renderer.setElementClass(this.elementRef.nativeElement, this.align, !!this.align);
}

Wrap the method inside of an if statement to make sure we are not adding / removing a class that is undefined:

handleAlign(previousValue?: string) {
  if (previousValue) {
    this.renderer.setElementClass(this.elementRef.nativeElement, previousValue, false);
  }
  if (this.align) {
    this.renderer.setElementClass(this.elementRef.nativeElement, this.align, true);
  }
}

I know this is not really a bug in the ng2-materialize library, but seeing that this is such an easy fix and probably not intended in the first place, I would love to create a PR for this.

Steps to Reproduce (for bugs)

Sadly I cannot provide a fiddle to reproduce this as this only affects Angular Universal, where the app is being rendered by the server and not the browser, and I wasn’t able to set one up for that. I’m using the pre-render strategy mentioned in the Angular universal starter kit, running Angular 6.0.0.

Your Environment

  • Version used: 1.8.1
  • Angular version used: 6.0.0
  • Browser Name and version: Server side (NodeJS v8.9.1)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
florianmrzcommented, Jun 4, 2018

Thanks for giving me the opportunity to help out!

1reaction
jfcerecommented, Jun 3, 2018

There is a lot of tests to correct if replacing for Renderer2. We were planing to remove it when migrating to Materialize-Css v1.0 since we will have to touch pretty much everything to remove JQuery and use plain JavaScript instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'classList' is undefined when using setElementClass in angular2
The issue I'm seeing in the results you're getting is that the component is not finding an ElementRef child component (the @ViewChild ...
Read more >
angular/angular - Gitter
I am trying to subscribe to my Laravel rest Api in Angular 2 using the Http ... I have a resolve class that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found