AngularJS – Display Default image if original image does not exists with example

AngularJS – Display Default image if original image does not exists with example

In this post we will give you information about AngularJS – Display Default image if original image does not exists with example. Hear we will give you detail about AngularJS – Display Default image if original image does not exists with exampleAnd how to use it also give you demo for it if it is necessary.

We always require to check image is exists or not in given path, If not exists then we have to set default image. If we work on PHP or any framework then we check condition or something else for image exists of not to check But If you are working on AngularJS framework then we can do it using “directive”.

AngularJS directive through we can simple check image is exists or not on the given url path. In this example i use ng-src and err-src attribute to manage image, directive will check ng-src url image is exists or not, if not then it will set err-src image as default.

So, I give you very basic example and you can simply understand how to use it, So let’s see bellow example:

Example:

Also see:AngularJS – update bind ng-model input value from Jquery Code

<!DOCTYPE html>

<html>

<head>

<title>AngularJS set ng src default image</title>

<script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

</head>

<body>


<div ng-app="mainApp" ng-controller="myController" id="mainController">

<img ng-src="{{realImage}}" style="width:200px" err-src="{{ myDefaultImage }}">

<img ng-src="{{noImage}}" style="width:200px" err-src="{{ myDefaultImage }}">

</div>


<script type="text/javascript">


var app = angular.module("mainApp", []);


app.controller('myController', function($scope, $timeout) {

$scope.myDefaultImage = 'https://onlinecode.org/upload/Laravel-5-comman.png';

$scope.realImage = 'https://onlinecode.org/upload/Laravel-mailchimp.png';

$scope.noImage = 'https://onlinecode.org/upload/no-image-available.png';

});


app.directive('errSrc', function() {

return {

link: function(scope, element, attrs) {

element.bind('error', function() {

if (attrs.src != attrs.errSrc) {

attrs.$set('src', attrs.errSrc);

}

});

attrs.$observe('ngSrc', function(value) {

if (!value && attrs.errSrc) {

attrs.$set('src', attrs.errSrc);

}

});

}

}

});


</script>


</body>

</html>

Maybe it can help you…

Hope this code and post will helped you for implement AngularJS – Display Default image if original image does not exists with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

6  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US