让我们说,我有这个项目在列表中与角点击事件。
<a data-id='102' ng-click='delete()'>Delete</a>
如何获得数据/信息,如果这样?
$scope.delete = function() {
var id = $(this).attr('data-id');
console.log(id); // I want to get 102 as the result
if (confirm('Are you sure to delete?')) {
$('#contactsGrid tr[data-id="' + id + '"]').hide('slow');
}
};
解决方法
正确的解决方案是将id作为参数传递给delete函数
<a data-id='102' ng-click='delete(102)'>Delete</a>
然后
$scope.delete = function(id) {
console.log(id); // I want to get 102 as the result
if (confirm('Are you sure to delete?')) {
$('#contactsGrid tr[data-id="' + id + '"]').hide('slow');
}
};
这不应该做,而只是为了演示
在ng-click中你可以使用$事件获得事件,所以
<a data-id='102' ng-click='delete($event)'>Delete</a>
然后
$scope.delete = function (e) {
var id = $(e.target).data('id');
console.log(id); // I want to get 102 as the result
};
演示:Fiddle