5

I have this jsFiddle: http://jsfiddle.net/HMZuh/1/

Which contains this html

<div ng-app ng:controller="ShowHideController">
    <div ng-show='showMe'>
        <img ng-src="{{imageSource}}"/>
    </div>
    <button ng-click='showImage()'> show image </button>
<div>

and this script:

function ShowHideController($scope) {
    $scope.showMe = false;

    $scope.imageSource = '';

    $scope.showImage = function(){
        $scope.showMe = true;
        $scope.imageSource = 'https://www.google.com/images/srpr/logo3w.png';
    }
}

I'm getting a 404, image not found when the source has not yet been set, is there any way of preventing this when showMe is false?

3
  • Not having this problem - wondering if it's a browser issue. What browser are you trying this with? Commented Sep 10, 2012 at 15:48
  • I tried it on chrome and firefox - both latest versions Commented Sep 10, 2012 at 19:26
  • Sorry mate, tried this on a local project from scratch, never got the 404. Commented Sep 10, 2012 at 20:34

2 Answers 2

3

To solve this you can:

  1. Use ng-repeat http://jsfiddle.net/bGA4T/
  2. Use $compile and declare your own directive
  3. Write your own ng-src directive
  4. ...

I think there are many ways to solve this.

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

1 Comment

Thanks, I ended up using ng-repeat which 'solved' the issue. Not quite what I would have liked, but it worked=)
3

I improved on this by using ui-if from http://angular-ui.github.com/ Instead of hiding/showing using ng-hide/ng-show, ui-if simply does not render the element.

<div ui-hide='ImageHasBeenUploaded'>
    <img ng-src='/some/image/path/{{imageName}}/>
</div>

Comments

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.