AngularJS Dynamically Change Image SRC on Click Example

By Hardik Savani October 20, 2023 Category : Angular

In this example, i want to show you how to change image src dynamically on image or button click event in angularjs. you will learn dynamically change the src attribute of image element when clicked using the ng-src directive in angular js.

Here, i will give you one example to change dynamic image ng-src. we will create one json array and change dynamically image when user will click on image.

So, let's see bellow code for change image on click in angular js.




<title>Angularjs dynamically change image src on click -</title>

<script type="text/javascript" src=""></script>



<div ng-app="MyApp" ng-controller="MyController">

<img ng-src="{{Image.Photo}}" ng-click="ImageClick()" style="height: 200px; width: 300px;

cursor: pointer" />

<p>Click on Image</p>



<script type="text/javascript">

var app = angular.module('MyApp', []);

app.controller('MyController', function ($scope, $interval) {

$scope.Images = [

{ Id: 1, Photo: "" },

{ Id: 2, Photo: "" },

{ Id: 3, Photo: "" },

{ Id: 4, Photo: "" }


$scope.Image = $scope.Images[0];

var index = 1;

$scope.ImageClick = function () {

$scope.Image = $scope.Images[index];


if (index > $scope.Images.length - 1) {

index = 0






I hope it can help you...