2016-09-04 17 views
0

我正在尋找一個上下文菜單指令,可以告訴我實際點擊了什麼,所以我可以對它做出反應。Angularj JS上下文菜單

this但該模塊的問題是,我無法弄清楚如何唯一標識被點擊的元素。所以我可以做正確的邏輯就可以了:

我需要的東西,我可以傳遞到回調函數

控制器:

$scope.items.Data = [{Code:'row1', Name:'Name1'},{Code:'row2', Name:'Name2'}] 
$scope.items.Actions = [ 
    { 
     Code: 'Action1', 
     Name: 'First action' 
    }, 
    { 
     Code: 'Action2', 
     Name: 'Second action' 
    } 
] 

var actionList = []; 
for (var i = 0; i < $scope.items.Actions.length; i++) { 

      actionList.push($scope.items.Actions[i].Name, function ($itemScope,$event) { 
       console.log('Action executed', $scope.objectId, $itemScope,$event,$itemScope.$index); 
       // Need access to $scope.items.Actions[i].Code or at least the index of the array 
      }); 
} 
$scope.contextOptions = actionList; 

HTML:

<table> 
    <thead> 
     <tr> 
      <td>Code</td> 
      <td>Name</td>  
     </tr> 
    </thead> 
    <tbody> 
     <tr ng-repeat="row in items.Data" context-menu="contextOptions"> 
      <td>{{row.Code}}</td> 
      <td>{{row.Name}}</td> 
     </tr> 
    </tbody> 
</table> 
+0

你能展示更多的代碼,包括HTML嗎? – cnorthfield

+0

添加了更多的代碼,基本上我需要確定被點擊的操作 – Jester

+0

'$ scope.items'中屬性'Data'的位置? – cnorthfield

回答

1

我正在使用相同的東西,在編制索引時沒有問題。 而不是使用

for (var i = 0; i < $scope.items.Actions.length; i++) {}

變化它

$scope.items.Actions.forEach(function(action, index){ actionList.push(action.Name, function ($itemScope,$event) { console.log('Action executed', $scope.objectId, $itemScope,$event,index); }); })

讓我知道這是你在找什麼。

+0

Ty是解決我的問題的方法 – Jester