Using JSON.stringify in an expression in Angular2 template

Template code doesn’t have access to all javascript, only component properties and methods. I think it would be best to create a ‘stringify’ method on your component, but you could just set a JSON property:

public constructor() {
  this.JSON = JSON;
}

Also I think your expression is backwards. NgClass takes the css class as the property name and a true/false value to tell whether that class is on the element, and it needs to be in brackets:

<div [ngClass]="{'div-show': JSON.stringify(obj1) != JSON.stringify(obj2)}"></div>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)