Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target ng-show on specific items in ng-repeat?

http://plnkr.co/edit/aiGCPJEwTj0RWnuoCjsW?p=preview

I want the remove button to only show the popover for that item.

How would you go about this? HTML:

<li ng-repeat="acc in accounts">
    <div class="well well-sm">
        <div class="popover-remove" ng-show="popoverRemove">Click to remove item</div>
        <h4>{{acc.label}}</h4>
        <button class="btn btn-default"
                ng-mouseover="showPopover()"
                ng-mouseleave="hidePopover()">Remove</button>
    </div>
</li>

Angular Controller:

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

.controller('AccountsCtrl',
    ['$scope',
    function($scope) {

    var vs = $scope;

        vs.accounts = [
            {
                id: '1',
                label: 'Bitage'
            },
            {
                id: '2',
                label: 'Blockchain.info'
            },
            {
                id: '3',
                label: 'Coinbase wallet'
            },
            {
                id: '4',
                label: 'Xapo wallet'
            }
        ];

        vs.showPopover = function() {
            vs.popoverRemove = true;
        };

        vs.hidePopover = function() {
            vs.popoverRemove = false;
        };

    }]);

enter image description here

like image 511
Leon Gaban Avatar asked Mar 17 '23 14:03

Leon Gaban


2 Answers

Plunker for you

The thing is that ng-repeat creates it's own scope.So, 'popoverRemove' is the local variable for each scope.You can set true or false to local variable by sending context to controller of that particular element of ng-repeat and set it's value using 'this'.

<button class="btn btn-default"
                    ng-mouseover="showPopover(this)"
                    ng-mouseleave="hidePopover(this)">Remove</button>

vs.showPopover = function(context) {
    context.popoverRemove = true;
};

vs.hidePopover = function(context) {
    context.popoverRemove = false;
};
like image 139
squiroid Avatar answered Apr 01 '23 07:04

squiroid


You can also create a property on each iteration like so:

Instead of calling a function on the scope I simply set the value of well.show to true/false, but you get the jist of the idea!

    <li ng-repeat="acc in accounts track by $index">
      <div class="well well-sm">
        <div class="popover-remove" ng-show="well.show">Click to remove item</div>
        <h4>{{acc.label}}</h4>
        <button class="btn btn-default"
                ng-mouseover="well.show=true"
                ng-mouseleave="well.show=false">Remove</button>
      </div>
    </li>

Plunker updated

like image 39
Mark Pieszak - Trilon.io Avatar answered Apr 01 '23 08:04

Mark Pieszak - Trilon.io