我正在使用Angular 1.5.6。
我有一个检查双击的指令:
angular.module('redMatter.analyse')
.directive('iosDblclick',
function () {
var DblClickInterval = 300; //milliseconds
var firstClickTime;
var waitingSecondClick = false;
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.bind('click', function (e) {
if (!waitingSecondClick) {
firstClickTime = (new Date()).getTime();
waitingSecondClick = true;
setTimeout(function () {
waitingSecondClick = false;
}, DblClickInterval);
}
else {
waitingSecondClick = false;
var time = (new Date()).getTime();
if (time - firstClickTime < DblClickInterval) {
scope.$apply(attrs.iosDblclick);
}
}
});
}
};
});
我在这里使用它:
<div ios-dblclick="onDoubleClick($event, graph)" ></div>
graph
是ng-repeat
指令中的对象。在onDoubleClick中,我需要访问$event
:
$scope.onDoubleClick = function($event, graph){
console.log('in onDoubleClick and arguments are ', arguments);
var element = $event.srcElement;
但是,我不确定如何将指令中的事件传递给onDoubleClick
。在控制台日志中,参数打印为:
[undefined, Object]
Object
为graph
的位置。我怎样才能传回活动?
答案 0 :(得分:2)
<强>已更新强>
http://jsfiddle.net/ADukg/14470/ - 工作范例。
因此,您可以将函数传递给指令,如下所示:
<div ios-dblclick="onDoubleClick" ios-dblclick-arg="graf" ></div>
在你的指令中:
return {
restrict: 'A',
scope: {
myCallback: '=iosDblclick',
graph: '=iosDblclickArg'
},
link: function (scope, element, attrs) {
element.bind('click', function (e) {
if (!waitingSecondClick) {
firstClickTime = (new Date()).getTime();
waitingSecondClick = true;
setTimeout(function () {
waitingSecondClick = false;
}, DblClickInterval);
}
else {
waitingSecondClick = false;
var time = (new Date()).getTime();
if (time - firstClickTime < DblClickInterval) {
scope.myCallback(e, scope.graph)
}
}
});
}
}
答案 1 :(得分:2)
由于您可以使用locals
方法传递$eval
,因此请在调用attrs.iosDblclick
时考虑使用它。在内部,它使用$parse
API来评估方法,并使用local作为参数。
scope.$eval(attrs.iosDblclick, {$event: e});
另见
答案 2 :(得分:0)
您可以将回调传递给指令:onDoubleClick: '&'
- 隔离范围
webApp.directive('iosDblclick',
function () {
var DblClickInterval = 300; //milliseconds
var firstClickTime;
var waitingSecondClick = false;
return {
restrict: 'A',
scope: {
onDoubleClick: '&'
},
link: function (scope, element, attrs) {
element.bind('click', function (e) {
if (!waitingSecondClick) {
firstClickTime = (new Date()).getTime();
waitingSecondClick = true;
setTimeout(function () {
waitingSecondClick = false;
}, DblClickInterval);
}
else {
waitingSecondClick = false;
var time = (new Date()).getTime();
if (time - firstClickTime < DblClickInterval) {
scope.onDoubleClick({data: {e:e, val: "someValue"}});
}
}
});
}
};
});
HTML在哪里:
<div ios-dblclick on-double-click="onDoubleClick(data)" ></div>
和控制器:
$scope.onDoubleClick = function(data){
var element = data.e.srcElement;
}