Hi
I have below code and i want if value is null in fMobileNo then in Html Hyphen should be displayed
if (this.fatherData && this.fatherData.phoneDetails) {
this.fMobileNo = this.fatherData.phoneDetails.find((c:any) => c.phoneType === 'MOBILE');
}
Thanks
Gowtham CpPosted Sep 1, 2024, 5:25 AM
Hi!
To display a hyphen when
fMobileNoisnull, update your TypeScript like this:And in your HTML, use:
This will show a hyphen if
fMobileNoisnull. Hope that helps!Jignesh KumarPosted Aug 30, 2024, 3:44 AM
Hello Ramco,
You can make the below changes,
html file :
Adarsh NigamPosted Aug 29, 2024, 3:52 PM
You can use the Elvis operator (also known as the nullish coalescing operator) in your HTML template to achieve this. Here's an example:
{{ fMobileNo ? fMobileNo : '-' }}
This will display the value of
fMobileNoif it's not null or undefined, and a hyphen (-) if it is.Alternatively, you can use a ternary operator:
{{ fMobileNo !== null ? fMobileNo : '-' }}
Both of these approaches will render a hyphen if
fMobileNois null or undefined.If you want to handle this in your TypeScript code instead, you could do something like this:
if (this.fatherData && this.fatherData.phoneDetails) {
this.fMobileNo = this.fatherData.phoneDetails.find((c:any) => c.phoneType === 'MOBILE');
if (!this.fMobileNo) {
this.fMobileNo = '-';
}
}
Meet ShahPosted Aug 29, 2024, 1:48 PM
Try this.
if (this.fatherData && this.fatherData.phoneDetails) {
const mobile = this.fatherData.phoneDetails.find((c: any) => c.phoneType === 'MOBILE');
this.fMobileNo = mobile ? mobile.number : null; // Assuming `number` is the property holding the phone number
}
HTML code
{{ fMobileNo || '-' }}
Amit MohantyPosted Jul 18, 2024, 12:15 PM
Try this
Vishal JoshiPosted Jul 18, 2024, 8:46 AM
Hello Ramco
Try the below code to check if null then show Hyphen
Thanks