{"id":16,"date":"2014-09-28T15:00:32","date_gmt":"2014-09-28T09:30:32","guid":{"rendered":"http:\/\/subashselvaraj.com\/?p=16"},"modified":"2025-06-21T07:27:17","modified_gmt":"2025-06-21T07:27:17","slug":"angularjs-fallback-image-directive","status":"publish","type":"post","link":"https:\/\/subashselvaraj.com\/index.php\/2014\/09\/28\/angularjs-fallback-image-directive\/","title":{"rendered":"Angularjs fallback image directive"},"content":{"rendered":"<p>Using fallback image with &lt;img&gt; tag is common scenario to show a default image if the original image is not loaded. In angular you can achieve this by creating a fallback directive like below,<\/p>\n<pre class=\"brush:js\">angular.module('fallback',[]).directive('fallbackSrc', function () {\n    return{\n        link: function postLink(scope, element, attrs) {\n            element.bind('error', function () {\n                angular.element(this).attr(\"src\", attrs.fallbackSrc);\n            });\n        }\n    }\n});\n<\/pre>\n<pre class=\"brush:html\">&lt;img ng-src=\"{{url}}\" fall-back-src=\"default.jpg\"\/&gt;<\/pre>\n<p>What it does is listen for the onError event of the original image, then replace it with the default image if it fails to load.<\/p>\n<p>But drawback of this directive is that it will show broken image until the original image&#8217;s onError method is called, then it will assign default image source. To overcome this, I have created &nbsp;the below directive,<\/p>\n<pre class=\"brush:js\">angular.module('fallback', []).directive('actualSrc', function () {\n    return{\n        link: function postLink(scope, element, attrs) {\n            attrs.$observe('actualSrc', function(newVal, oldVal){\n                 if(newVal != undefined){\n                     var img = new Image();\n                     img.src = attrs.actualSrc;\n                     angular.element(img).bind('load', function () {\n                         element.attr(\"src\", attrs.actualSrc);\n                     });\n                 }\n            });\n\n        }\n    }\n});\n<\/pre>\n<pre class=\"brush: html\">&lt;img actual-src=\"{{url}}\" ng-src=\"default.jpg\"\/&gt;<\/pre>\n<p>This will assign the default image as source and checks for the load event of original image,&nbsp;then&nbsp;substitutes it if the image is loaded. By using this user won&#8217;t see broken image at all.<\/p>\n<p>Find the full directive in this <a href=\"https:\/\/github.com\/sesubash\/angular-fallback-image-directive\" target=\"_blank\" rel=\"noopener\">Git repository<\/a><\/p>\n<p>Hope it helps some one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using fallback image with &lt;img&gt; tag is common scenario to show a default image if the original image is not loaded. In angular you can achieve this&#8230; <\/p>\n","protected":false},"author":1,"featured_media":221,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_themeisle_gutenberg_block_has_review":false},"categories":[8,4],"tags":[15],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":12,"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":222,"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions\/222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/media\/221"}],"wp:attachment":[{"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/subashselvaraj.com\/index.php\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}