我有一个带有多个数据-*属性的锚标记,这个锚将使用ng-重复多次重复。当单击单个元素时,我想获取属性的值。
我使用JQuery获取属性值,使用$(this).attr(' attribute ')很容易。
但是,这只适用于页面上的一个按钮。当使用ng-重复呈现多个按钮时,此操作不起作用。我假设这是因为多个按钮以相同的属性名称呈现,而jquery不知道如何区分它们。如何获得单击的元素的数据-*属性值?
//JQuery
//Works when only a single editButton is on the page but not when multiple buttons are rendered using ng-repeat
$('.editButton').on('click', function () {
var filepath_id = $(this).attr('data-filepath-id');
var requested_filepath = $(this).attr('data-requested-filepath');
var requested_filename = $(this).attr('data-requested-filename');
})
//HTML
//Confirmed that each editButton element has the correct attribute values depending on the item
tr(ng-repeat='item in vm.items')
a.editButton.btn.btn-primary(
data-toggle="modal",
data-filepath-id="{{item.id}}",
data-requested-filepath="{{item.requested_filepath}}",
data-requested-filename="{{item.filename}}",
href="#editFilepathPopup"
) Edit
发布于 2017-12-04 23:39:39
觉得我能帮上忙。在angularJS中,事件侦听器是html元素中的一个属性。例子:
ng-repeat = "item in vm.items" ng-click="onClickFunction();"
如果要获取特定项的属性,请将该项作为参数放在事件函数中。
ng-repeat = "item in vm.items" ng-click="vm.onClickFunction(item);"
现在,在您的角度控制器中,您创建了单击函数。在这里,您可以执行更多的逻辑操作,例如将值设置为控制器值。
onClickFunction(itemParam) {
console.log("this is itemParam", itemParam);
var x = itemParam.requested_filepath;
this.x = itemParam.requested_filepath;
}
https://stackoverflow.com/questions/47643292
复制相似问题