Evripides Kyriacou
Evripides Kyriacou

Reputation: 85

ngif if is false go to loop

I am trying to check the filename variable if has value but if the value is null is trying to execute the ngfor

<h2>{{news.content}}</h2>


<ng-container *ngIf="filename">
    <div *ngFor="let file of filename">
        <img src="{{'http://127.0.0.1:8000/images/'+file}}"  style = "width: auto;margin: auto;display: block;">
    </div>
  </ng-container>

Upvotes: 1

Views: 204

Answers (3)

user10186186
user10186186

Reputation:

Check the filename length.

*ngIf="filename?.length > 0"

Upvotes: 2

Becario Senior
Becario Senior

Reputation: 714

To cast any value to boolean and make a secure check, please use double negation operator, like this:

*ngIf="!!filename"

This way,

if true -> false -> true
if 25 -> false -> true
if "Hello world" -> false -> true
if null -> true -> false
if undefined -> true -> false
if false -> true -> false 

Upvotes: 0

Mangesh Daundkar
Mangesh Daundkar

Reputation: 1046

try this,

*ngIf="filename.length!==0"

Upvotes: 0

Related Questions