简体   繁体   English

在Angular指令链接中向div添加onclick函数

[英]Adding onclick function to div in Angular directive link

I have an angular directive with a link scope. 我有一个带有链接范围的角度指令。

In that scope, I append a div. 在此范围内,我附加了一个div。 I would like to add an event listener to this div. 我想向该div添加一个事件监听器。

When I do so, the event listener is overridden by the ng-click native attributes of the directive. 当我这样做时,该指令的ng-click本机属性将覆盖事件侦听器。

What is the best way forward? 最好的前进方向是什么? Trying to use element to identify the element does not work as element compiles asynchronously. 由于元素异步编译,因此尝试使用element标识该元素不起作用。

Why not just use ng-click? 为什么不只使用ng-click?

link: function(scope, element, attrs) {
    scope.onHandleClick = function() {
        console.log('onHandleClick');
    };
}

<div ng-click="onHandleClick($event)"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM