Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check null value in variable in angular component

I have this div

<td><a *ngIf="data?.URL; else noData"  [href]="data?.URL" target="_blank">View
                            File</a>
                            <ng-template #noData>
                                <span> No File</span>
                            </ng-template>

                               
                        </td>

in some cases data.URL is null URL: "null" and else there will be file url but in my component in all case View File is displayed.

another issue is I have url like this https://egdomain/download_.jpg i want to display only the file in the link not the full url like download_.jpg

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any solution to fix this issue

>Solution :

  1. URL: "null" is a string and not a null value. So along with null value, you also need to compare "null" string as well if you want to filter it out.

Something like below:

*ngIf="data?.URL && data.URL !== 'null'; else noData"
  1. To play with the variables in view component in Angular, use {{ ... }} statements. Below is how you can trim the trailing / if present, split the whole string by / and get the last keyword (the file name) and display it:
<a [href]="...">{{data.URL.replace(/\/$/, '').split('/').pop()}}</a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading