0

I have an input field in Angular 7 for that i use trim () and toLowerCase() the logic works but some times in console it shows .trim() and toLowerCase() of undefined how to handle this errors please help.

             <input
             oninput="this.value = this.value.toLowerCase()"
             (blur)="supplierInfo.name = supplierInfo.name.trim()"
             [(ngModel)]="supplierInfo.name" 
              />

1 Answer 1

1

You can do null/undefined checks by using safe navigation operator ( ? )

For example:

(blur)="supplierInfo?.name = supplierInfo?.name?.trim()"

Adding the same for this.value

oninput="this.value = this.value?.toLowerCase()" - In this case, it the new value is null/undefined - then it will retain the same
oninput="this.value = this.value?.toLowerCase() || ''" - Will update the string to a blank string
Sign up to request clarification or add additional context in comments.

3 Comments

how to do for this one oninput="this.value = this.value.toLowerCase()"
Depends what you want. You can use this.value = this.value?.toLowerCase(). This will set the value to undefined. And you can also set it to blank using this.value = this.value?.toLowerCase() || ''
Ho to provide null check for this one {{ itemFornew.location.addressLine2 }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.