0

Suppose if i want to show image2 on click image1, how to do that in angularjs.I have tried using ng-click twice, but it does not work.

<div class="image-container" ng-if="item.image_url != ''">
  <img ng-src="{{API_URL+item.image_url}}"
      ng-click="templatesCtrl.showPrediction1($event,item1)"
      ng-click="templatesCtrl.showPrediction2($event,item2)"
      alt="cax-image">

  <div class="show-me" style="display: none;">
      <img ng-src="{{API_URL+item.image_url}}" />
  </div> 

Here i used two function for ng-click .

2
  • 1
    can you try this : ng-click="templatesCtrl.showPrediction1($event,item1);templatesCtrl.showPrediction2($event,item2)" Commented Nov 20, 2019 at 13:22
  • yeah, it works!@AhmetAmasyalı Commented Nov 21, 2019 at 4:54

2 Answers 2

1

You can simply use ng-show and ng-hide

check my code

var app = angular.module('myApp', []);
app.controller('customersCtrl', function() {
  var self = this;
  self.show_img1 = false;
  self.changeImg = function(ev){
    self.show_img1 = !self.show_img1;
  }
});
img {
    width: 50%;
    height: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="customersCtrl as cstCtrl">
    <div class="image-container">
        <div ng-hide="cstCtrl.show_img1">
            <label>Image1</label>
            <img ng-src="https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="cax-image" 
                 ng-click="cstCtrl.changeImg($event)">
        </div>
        <div ng-show="cstCtrl.show_img1">
            <label>Image2</label>
            <img ng-src="https://images.pexels.com/photos/326055/pexels-photo-326055.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" />
        </div>
    </div>
</body>

Sign up to request clarification or add additional context in comments.

Comments

0

You cant have two click events on the same item. You could add a toggle to show and hide the image based on the click like below

HTML

<div class="image-container" ng-if="item.image_url != ''">
         <img ng-src="{{API_URL+item.image_url}}" 
            ng-click="showPrediction1()" alt="cax-image">
   <div ng-if="showImage">
     <img ng-src="{{API_URL+item.image_url}}" />
   </div>
</div>  

AngularJS Controller

$scope.showImage = false;

$scope.showPrediction1 = function(){
   $scope.showImage = !$scope.showImage
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.