2015-04-01 88 views
4

http://plnkr.co/edit/aiGCPJEwTj0RWnuoCjsW?p=preview如何定位ng-repeat中特定項目的ng顯示?

我想刪除按鈕只顯示該項目的酥料餅。

你會怎麼做呢? 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> 

角控制器:

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

+1

嘗試[$指數](https://docs.angularjs.org/api/ng/directive/ngRepeat)。 – 2015-04-01 16:45:53

+2

[示例](http://plnkr.co/edit/F1QFgL65kj5v6q1dMGpM?p=preview)。 – 2015-04-01 16:49:03

+1

@AustinMullins真棒:)但有一件事保持,如果你會在ng-repeat上使用過濾器,那麼$ index會改變,並且不會被應用(只是爲了防範)。 – squiroid 2015-04-01 16:53:36

回答

4

Plunker

的事情是,NG-重複創建它自己scope.So, 'popoverRemove' 是每個作用域的局部變量。您可以通過向該特定元素的控制器發送上下文來將真或假設置爲局部變量使用'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; 
}; 
+0

這也適用! – 2015-04-01 16:49:59

+0

謝謝!哇這麼多的方式來完成這:)甚至發現了一個Angular-ui popover https://angular-ui.github.io/bootstrap/ – 2015-04-01 16:54:13

+0

@LeonGaban是啊:)角度是驚人的:) – squiroid 2015-04-01 16:55:28

2

您也可以在每次迭代創建一個屬性,像這樣:

而是呼籲範圍功能,我只需設置well.show至真/假值的,但你得到的將JIST理念!

<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

1

移動popOverRemove到每個帳戶。然後你可以從每個子範圍控制它。更新你的showPopover/hidePopover。

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

我已經在這裏更新

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

+0

這是一個正確的答案,但是你真的可以爲每個對象添加額外的屬性是一個不錯的選擇嗎? – squiroid 2015-04-01 16:54:47

+0

那麼,我已經在JSON中添加了popoverRemove以供您理解。但基本上當你使用ng-repeat中的popoverRemove時,角度會自動添加到childscope中,或者它存在於json中。您可以在調試工具中檢查childscope對象以進行驗證。所以它幾乎與保存在json中一樣 – Samir 2015-04-01 17:01:02

相關問題