3

I'm comparing a number of fields in an Angular 2 template and it works for same case properties but returns false for the same string on different cases. Is there a way to make it case insensitive perhaps through a simple pipe?

<div *ngIf="query?.firstName == address.foreName"></div>
Felix Too
  • 10,946
  • 5
  • 21
  • 23
  • Possible duplicate of [JavaScript case insensitive string comparison](https://stackoverflow.com/questions/2140627/javascript-case-insensitive-string-comparison) – Supamiu Jan 24 '18 at 16:14

3 Answers3

6

Use Angular pipe.

<div *ngIf="(query?.firstName | lowercase) === (address.foreName | lowercase)"></div>
Chybie
  • 3,646
  • 1
  • 17
  • 15
4

You should use === with toLowercase()

<div *ngIf="query?.firstName.toLowerCase() === address.foreName.toLowerCase()"></div>
Sajeetharan
  • 203,447
  • 57
  • 330
  • 376
1

If you are doing lot of string operations in your application, I suggest using a third party library like this one -

How to install

npm install --save string

Import

var S = require('string');

Ignorecase Compare String

var isEqual = S('ignoreCase').equalsIgnoreCase('IGNORECASE')
Akash
  • 4,132
  • 3
  • 24
  • 42