2

Below is the code :

<div ng-repeat="u in users">
<!--I will always receive a URL -->
<div ng-if="u.imageUrl"> <!--Here I want to check if the provided URL is active-->
 <!--Only seen if image url is live and active -->
</div>
<div>
<!--do some other things-->
</div>
</div>

e.g. : http://www.pacinno.eu/wp-content/uploads/2014/05/placeholder1.png is active URL but http://www.pacinno.eu/wp-content/uploads/2014/05/placeholder11.png is not. Also users may have many records as signin increases.

6
  • angular-img-fallback could this be something that you are looking? Commented Mar 22, 2016 at 7:56
  • If I'm correct angular-image-fallback is used to show alternate image. But I straightforwardly don't want to show that div. Commented Mar 22, 2016 at 7:58
  • <img ng-src may work in this scenario. Please check Commented Mar 22, 2016 at 7:59
  • I know about ng-src. But what if I want to hide my div ? Commented Mar 22, 2016 at 8:00
  • dont know if this would work but maybe fallback to 1x1 pixel image? Commented Mar 22, 2016 at 8:03

1 Answer 1

1

You can do that with a directive:

Here is the JSFiddle

JS:

.directive('userAvatar', function() {
    return {
        link: function(scope, element, attrs) {
            var placeholder = 'path/to/your/placeholder.jpg';

            scope.$watch(function() {
                return attrs.ngSrc;
            }, function (value) {
                if (!value) {
                    element.attr('src', placeholder);  
                }
            });

            element.bind('error', function() {
                element.attr('src', placeholder);  
            });
        }
    };
});

HTML:

<div ng-repeat="u in users">
    <div>
      <img ng-src="{{u.imageUrl}}" user-avatar />
    </div>
<div>

So now if the ng-src value is a 404 not found image it will be replaced with the default placeholder

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but I know this already. I don't want any alternate image. You can check my question that I'm want my div to be hidden if URL in u.imageUrl is not active or not found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.