2015-11-16 35 views
0

我需要在点击一个表中的行获得的价值和弹出式窗口中显示它获取价值并把它传递给弹出

HTML:

<md-data-table-container> 
    <table md-data-table class="md-primary" md-progress="deferred"> 
     <thead md-order="query.order" md-trigger="onorderchange"> 
      <tr> 
       <th name="Task To Be Done"></th> 
       <th name="Office"></th> 
       <th name="Due Date"></th> 
      </tr> 
     </thead> 
     <tbody ng-click="showAlert($event)"> 
      <tr ng-repeat="dessert in desserts.data" ng-click="showAlert($index)" flex-sm="100" flex-md="100" flex-gt-md="auto"> 
       <td>{{dessert.task}}</td> 
       <td></td> 
       <td>{{dessert.due_on}}</td> 
      </tr> 
     </tbody> 
    </table> 
</md-data-table-container> 

JS:

$scope.desserts = { 
    "count": 6, 
    "data": [{ 
     "task": "Frozen yogurt", 
     "type": "Ice cream" 
    }, { 
     "task": "Ice cream sandwich", 
     "type": "Ice cream" 
    }, { 
     "task": "Eclair", 
     "type": "Pastry" 
    }, { 
     "task": "Cupcake", 
     "type": "Pastry" 
    }, { 
     "task": "Jelly bean", 
     "type": "Candy" 
    }, { 
     "task": "Lollipop", 
     "type": "Candy" 
    }, { 
     "task": "Honeycomb", 
     "type": "Other" 
    }] 
}; 
$scope.showAlert = function (index) { 
    $scope.obj = $scope.desserts.data[2]; 
    $scope.task = $scope.obj.task; 
    alert($scope.task); 
    console.log($scope.task); 
}; 

问题在我的代码是,我可以得到我指定“。数据[2]”的数组上的值。实际上,当我点击表格中的一行时,我需要显示该值以弹出“sweetAlert”。是否有任何解决方案

+0

这不是很清楚。你有一个沙漠阵列,但没有显示它,你有一个点击调用$索引,但没有重复围绕它。甜蜜的警报来自哪里?你从不打电话或使用它?你怎么认为有人可能会告诉你什么是错的。用你的代码创建一个plunkr或jsfiddle。 – Shaun

+0

检查我的更新后的代码 –

+0

添加了答案。我不知道“sweetAlert”是什么意思。 – Shaun

回答

3

不要通过$index,因为如果底层数据发生变化,这可能会有点危险,在这种情况下,它只是迫使你重新从数组中获取物品。

将想要显示的实际项目传递回警报。

<tr ng-repeat="dessert in desserts.data" ng-click="showAlert(dessert)" flex-sm="100" flex-md="100" flex-gt-md="auto"> 
       <td>{{dessert.task}}</td> 
       <td></td> 
       <td>{{dessert.due_on}}</td> 
      </tr> 

不要分配给范围,除非你真的需要在别处。

$scope.showAlert = function (dessert) { 
    alert('Task:' + dessert.task); 
    // if you're just using a variable in this function, declare it locally 
    var dessertType = dessert.type; 
    console.log(dessertType); 
}; 

见$指数造成问题的例子:

http://codeutopia.net/blog/2014/11/10/angularjs-best-practices-avoid-using-ng-repeats-index/comment-page-1/

相关问题