Hi
I have below code and i want if value is null in fMobileNo then in Html Hyphen should be displayed
TypeScript
if (this.fatherData && this.fatherData.phoneDetails) {
this.fMobileNo = this.fatherData.phoneDetails.find((c:any) => c.phoneType === 'MOBILE');
}Thanks

Vishal JoshiRank #377 · 6.7K reputationPosted Jul 18, 2024, 4:46 AM EDT
Hello Ramco
Try the below code to check if null then show Hyphen
if (this.fatherData && this.fatherData.phoneDetails) { this.fMobileNo = this.fatherData.phoneDetails.some((c:any) => c.phoneType === 'MOBILE') ? this.fatherData.phoneDetails.find((c:any) => c.phoneType === 'MOBILE') : '-'; }Thanks
Amit MohantyRank #112 · 20K reputationPosted Jul 18, 2024, 8:15 AM EDT
Try this
if (this.fatherData && this.fatherData.phoneDetails) { const mobile = this.fatherData.phoneDetails.find((c: any) => c.phoneType === 'MOBILE'); this.fMobileNo = mobile ? mobile.phoneNumber : '-'; // Assuming mobile has a property 'phoneNumber' }Adarsh NigamRank #950 · 2.1K reputationPosted Aug 29, 2024, 11:52 AM EDT
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 ShahRank #1,739 · 868 reputationPosted Aug 29, 2024, 9:48 AM EDT
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 || '-' }}
Jignesh KumarRank #91 · 22.9K reputationPosted Aug 29, 2024, 11:44 PM EDT
Hello Ramco,
You can make the below changes,
this.fMobileNo = this.fatherData?.phoneDetails?.find((c: any) => c.phoneType === 'MOBILE') ?? null;html file :
Gowtham CpRank #1,552 · 1K reputationPosted Sep 1, 2024, 1:25 AM EDT
Hi!
To display a hyphen when
fMobileNoisnull, update your TypeScript like this:this.fMobileNo = this.fatherData?.phoneDetails.find((c: any) => c.phoneType === 'MOBILE') || null;And in your HTML, use:
This will show a hyphen if
fMobileNoisnull. Hope that helps!