angularjs - 如何将 ng-show 定位到 ng-repeat 中的特定项目上?

标签 angularjs angularjs-ng-repeat angular-ngmodel

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

我希望删除按钮仅显示该项目的弹出窗口。

你会怎么做呢? 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

最佳答案

Plunker为你

问题是 ng-repeat 创建了它自己的作用域。因此,“popoverRemove”是每个作用域的局部变量。您可以通过将上下文发送到 ng-repeat 的特定元素的 Controller 来将 true 或 false 设置为局部变量并使用“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;
};

关于angularjs - 如何将 ng-show 定位到 ng-repeat 中的特定项目上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29396270/

相关文章:

javascript - AngularJS : Use factory inside a controller

angularjs - 为什么我得到 Lexerr :unterminated quote error?

javascript - AngularJS 获取单击的元素的 id 并在 Controller 中使用

javascript - 设置变量时,Angular Js ng-init 在 ng-repeat 内无法正常工作

angularjs - 如何查看ng-repeat中包含的ng-model?

angularjs - 即使多个 Angular 客户端失去了与中央服务器的连接,它们如何能够在它们之间进行通信?

javascript - angular-tree-control 删除整体填充

javascript - 在 ng-repeat 中的另一行的特定索引处插入行

javascript - 在 AngularJS 中将 rangeSlider 与搜索过滤器结合起来

用于自定义可重用组件的 Angular2 数据绑定(bind)