4

I'm trying to toggle a button image when a user clicks it. I prefer to use angularjs instead of jquery if possible. Right now I have a working version which toggles the image when clicked, the only problem is it changes ALL the images on click. How do I reduce the scope or pass in the src attribute for the img element?

    <div ng-repeat="merchant in merchants">
      <div class="followrow">
        <a ng-click="toggleImage()"><img id="followbutton" ng-src="{{followBtnImgUrl}}"  /></a>
      </div>
    </div>

app.controller('FollowCtrl', function CouponCtrl($scope) {
    $scope.followBtnImgUrl = '/img1'


    $scope.toggleImage = function () {
        if ($scope.followBtnImgUrl === '/img1.jpg') {
            $scope.followBtnImgUrl = baseUrl + '/img2.jpg';
        } else {
            $scope.followBtnImgUrl = 'img1.jpg';
        }
    }
});

Can I pass in the img src attribute the function like toggleImage(this.img.src) or similar?

3 Answers 3

5
<div ng-repeat="merchant in merchants">
  <div class="followrow">
    <a ng-click="toggleImage(merchant)"><img id="followbutton" ng-src="{{merchant.imgUrl}}"  />
    </a>
  </div>
</div>

.

app.controller('FollowCtrl', function CouponCtrl($scope) {
    $scope.followBtnImgUrl = '/sth.jpg'
    $scope.merchants = [{imgUrl: "img1.jpg", name:"sdf"}, 
                         {imgUrl: "img2.jpg", name: "dfsd"}];


     $scope.toggleImage = function(merchant) {
         if(merchant.imgUrl === $scope.followBtnImgUrl) {
             merchant.imgUrl = merchant.$backupUrl;
         } else {
             merchant.$backupUrl = merchant.imgUrl;
             merchant.imgUrl = $scope.followBtnImgUrl;
         }
    };
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is another valid way to do it. Be sure to reference the merchant in the ng-click="toggleImage(merchant) call.
4

What you want is a new scope for each followrow. As your code stands, there's only one scope that all of the followrows are referencing.

A simple answer is to create a new controller that you attach to each followrow:

<div class="followrow" ng-controller="ImageToggleCtrl">...</div>

And then move the image toggling logic to that new controller:

app.controller('ImageToggleCtrl', function($scope) {
  $scope.followBtnImgUrl = '/img1';
  $scope.toggleImage = function() { /* the toggling logic */ };
});

Now, a new scope will be instantiated for each row, and the images will toggle independently.

2 Comments

You do not need to create a new controller at all, it is a very simple case well handled by angularjs. Angularjs creates a new scope for each item in ng-repeat. You can store the image url in the object and change it accordingly.
@Umur Kontacı , could you please elaborate with some example.
0

I just added two clickable images:

<div ng-app="FormApp" ng-controller="myController" max-width="1150px;" width="1150px;" >
    <input ng-show="ShowDown" type="image" style="width:250px; height:40px;" src="~/Content/Images/contactShow.png" ng-click="ShowHide()"/>
    <input ng-show="ShowUp" type="image" style="width:250px; height:40px;" src="~/Content/Images/contactHide.png" ng-click="ShowHide()" />
</div>

They toggle eachothers visibility. At page load one is visible, one is not, and both clickable images call the same function:

<script type="text/javascript">
    var app = angular.module('FormApp', [])
    app.controller('myController', function ($scope) {
        $scope.ShowDown = true;
        $scope.ShowUp = false;

        $scope.ShowHide = function () {
            $scope.ShowDown = $scope.ShowDown ? false : true;
            $scope.ShowUp = $scope.ShowUp ? false : true;
        }
    });
</script>

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.