Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing image from rendering in AngularJs

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?

like image 658
Hugo Forte Avatar asked Sep 10 '12 14:09

Hugo Forte


People also ask

How to add images to an angular application?

This tutorial shows multiple examples in Angular, One example is to load images using the img tag Another example is to bind the src attribute to the assets folder. Normally, Images are placed in the src/assets folder in the Angular application.

Why are my images not displaying correctly in angular?

If images do not display correctly, check src/assets folder is included inside the assets attribute in the angular.json file of your angular application. Check path relevant HTML file location with the assets folder

Which tag is used to display an image on an angular?

The Img tag is used to display an image on an angular application. It is declared in the HTML template file. src contains the absolute path of images that referred from the HTML file alt contains string content to display on image for SEO purpose

Why is my navigation bar width so low in AngularJS?

Ultimately, the issue was that the width of the navbar-header and the profile-dropdown elements were being calculated using JavaScript in an AngularJS directive after the DOM was loaded and whenever there was a page resize in the link function, something like this.


2 Answers

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.

like image 157
Ivan Dyachenko Avatar answered Oct 06 '22 01:10

Ivan Dyachenko


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>
like image 28
Hugo Forte Avatar answered Oct 06 '22 01:10

Hugo Forte