ng-Src Directive In AngularJS

Many times in development, we have to show images on the page. Sometimes, image path is coming from the client side scripting language (it may be coming from database).

This is the age of Angularjs. When we work with Angularjs and we want to show the images in the page, we simply put <img src=”path of image”>.

If we consider an Output, i'ts working fine and there is no doubt about it but in the the Browser console, we will get 404 (not found) error.
Output

To remove this kind of an error, we have ng-Src. Before proceeding with ng-Src, I want to show you an example of how this error comes. Look at the example, given below:

Script.js

  1. var testApp = angular  
  2.                 .module("testModule", [])  
  3.                 .controller("testController"function ($scope) {  
  4.                     var animal = {  
  5.                         name: "CAT",  
  6.                         color: "White",  
  7.                         picture: "/images/cat.jpg",  
  8.                     };  
  9.   
  10.                     $scope.animal = animal;  
  11.   
  12.                 });  
Index.html
  1. <html ng-app="testModule">  
  2. <head>  
  3.     <title></title>  
  4.     <script src="scripts/angular.min.js"></script>  
  5.     <script src="scripts/js/script.js"></script>  
  6. </head>  
  7. <body>  
  8.       
  9.     <div ng-controller="testController">  
  10.         Name: {{animal.name}}  
  11.         <br />  
  12.         Color: {{animal.color}}  
  13.         <br />  
  14.         <img src="{{animal.picture}}" />  
  15.   
  16.     </div>  
  17. </body>  
  18. </html>  
In the example, mentioned above, we have one animal class, which has three properties, Name, Color and Picture. We have already put the value in it. Based on our model binder,  we called these properties into the page. For image, I use basic <img> input type HTML control. When I am going to run this, I will get the output, given below:

Output

If you see your Browser console, you will get this error.

Failed to load the resource: the Server responded with a status of 404 (Not Found).

Now, the question arises, why does this error come up and what is the solution for this?

Reason- When DOM is parsed, it tries to retrieve  the image from the Server. This point of time, Angularjs binding expression {{ model }} , which is specified with src attribute is not evaluated due to which we will get 404 not found error.

Solution- Solution is ng-Src. Use ng-Src, instead of src attribute in the images, with the use of this Angular directive, request will issue only after Angular has evaluated the binding expression.

Use the code, given below, for ng-Src:
  1. <html ng-app="testModule">  
  2. <head>  
  3.     <title></title>  
  4.     <script src="scripts/angular.min.js"></script>  
  5.     <script src="scripts/js/script.js"></script>  
  6. </head>  
  7. <body>  
  8.     <div ng-controller="testController">  
  9.         Name: {{animal.name}}  
  10.         <br />  
  11.         Color: {{animal.color}}  
  12.         <br />  
  13.         <img ng-src="{{animal.picture}}" />  
  14.   
  15.     </div>  
  16. </body>  
  17. </html>  
If you check your Browser console now, you will not get 404 not found. Thus, this is the use of ng-Src.

Defination: ng-Src- This directive overrides the original src attribute of an <img> element.

 

Up Next
    Ebook Download
    View all
    Learn
    View all