[英]How to pass AngularJS ng-repeat Variable to Javascript click event
I would like to know how to pass Angular ng-repeat variable to my Javascript onclick event 我想知道如何将Angular ng-repeat变量传递给我的Javascript onclick事件
My angular repeater 我的角度中继器
<div class="data" data-ng-repeat="sub in subs" >
<button onclick="ConfirmDialog(sub.SubID)">Cancel</button>
{{ sub.someOtherProperty}}
{{ sub.someOtherProperty}}
{{ sub.someOtherProperty}}
</div>
My script function 我的脚本功能
<script type='text/javascript'>
function ConfirmDialog(subID) {
console.log('Succesfully submitted id: ', subID);
});
</script>
The error : sub is not defined (in the onclick() call from the button element) all other properties show as expected. 错误: sub未定义 (在button元素的onclick()调用中)所有其他属性均按预期显示。
You are running straight javascript onclick instead of ng-click. 您正在直接运行javascript onclick而不是ng-click。
move confirmDialog into your controller and ng-click can attach to it. 将confirmDialog移动到您的控制器中,然后按一下ng即可将其附加。
<div class="data" data-ng-repeat="sub in subs" >
<button ng-click="ConfirmDialog(sub.SubID)">Cancel</button>
</div>
$scope.ConfirmDialog = function (subID) {
console.log('Succesfully submitted id: ', subID);
});
If you don't want to use ng-click, you still have a chance! 如果您不想使用ng-click,仍然有机会! Try the following
尝试以下
<tr ng-repeat="supplier in suppliers" id="{{supplier.id}}">
<button onclick="readVariable(this);">
</tr>
<script>
function readVariable(elem){
id = elem.parentNode.id;
console.log(id); //it works!
}
</script>
您应该改用ng-click来访问ng-repeat变量
<button ng-click="ConfirmDialog(sub.SubID)">Cancel</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.