Ng-show for avatar


I succesfull get a list of user and take your photo to make avatar in list.
I would set generic avatar if the user haven’t upload photo, but how is possible?

You can have two divs: one containing the user photo and the other containing the generic avatar. Then you can do an ng-if to decide which one gets added to the html. This also depends on how you’re getting the user photo. Is the photo in a JSON object as a string?

I’m getting the photo name by JSON string:


as you could see some user have the name of photo, others have 0 or empty value (it’s an old database)!

Now I write so:

<div class="list">
<a class="item item-avatar" href="#/app/showprof/{{result.user_id}}" ng-repeat="result in results track by $index">
<img ng-show="!=0" ng-src="{{}}/{{}}">
<img ng-if="" src="img/generic_avatar.png">
  <p>{{result.distanza}} km</p>

but no avatar is showen! If I remove

<img ng-if="" src="img/generic_avatar.png"> 

I see only image different from 0!

You have a few problems with the above code. The 0 is a string and you are comparing it as an integer, the second comparison is an assignment not a compare, and you aren’t accounting for empty values.

I put together this codepen with your code to show you how to check for all of the above. Hope that helps!


thank you so much this works for me! :wink:

1 Like

sorry, but I use your code to show if a message it’s send or receive and put in ng-repeat:

<span ng-if="messaggi.per = messaggi.user_id">from:</span>
<span ng-if="messaggi.per != messaggi.user_id">to:</span>

but with this results:


it’s always show only “from:”!!!

Using a single “=” results in an assignment. If you want to compare that two things are equal to each other you need to use two “==”. It will always show “from” because you are assigning user_id to per each time.

But how could use ng-if into repeat element???