这是因为 API 在网格加载到 DOM 时进行注册。由于您有两个具有相同范围的网格,因此当第二个加载时,它的 API 变为 $scope.gridApi 定义的内容。因此,您对该 API 所做的任何事情都与第二个网格实例进行交互。
这里的解决方案是将网格分开,可能是 $scope.gridOptions1、$scope.gridOptions2。对于两者都必须发生的任何事情,您可以在函数内独立调用每个 API。
这就是我要做的:
http ://plnkr.co/edit/nLLR63L4H4NqLoZsGpkS?p=preview
对于您的提交函数,假设您想要一个包含结果的数组,我已经更新了该函数,以便它提供一个结果:
$scope.submitData = function() {
var selectedRows1 = $scope.gridApi1.selection.getSelectedRows(),
selectedRows2 = $scope.gridApi2.selection.getSelectedRows(),
selectedRows = selectedRows1.concat(selectedRows2);
console.warn(selectedRows);
alert(selectedRows.length)
if (selectedRows.length === 0)
alert('Please select an item from grid');
else
alert(JSON.stringify(selectedRows));
}