How do I call a controller method onDrag()
from my directive
?
Html / (SVG)
<g ng-controller="EntityController as vm" draggable> ... </g>
Directive
app.directive('draggable', ['$document', function($document) {
return {
link: function(scope, element, attr) {
var startX = 0, startY = 0, x = 0, y = 0;
element.on('mousedown', function(event) {
event.preventDefault();
startX = event.pageX - x;
startY = event.pageY - y;
$document.on('mousemove', mousemove);
$document.on('mouseup', mouseup);
});
function mousemove(event) {
y = event.pageY - startY;
x = event.pageX - startX;
element.attr("transform", "translate(" + x + "," + y + ")");
// I want to call the controllers onDrag() here
// possibly passing in values
}
function mouseup() {
$document.off('mousemove', mousemove);
$document.off('mouseup', mouseup);
}
}
};
}]);
Controller
app.controller("EntityController", function () {
var vm = this;
vm.onDrag = function () {
// do stuff
}
});
If I am not doing this correctly I do not mind changing my approach, it may be better to pass in the method to be called in another html tag on the same g element, like a on-drag="vm.onDrag()";
Update
As per suggestions I have changed the code :
<g ng-controller="EntityController as vm" on-drag="vm.drag()" draggable>
app.directive('draggable', ['$document', function ($document) {
return {
scope: {
onDrag: '&'
},
link: ...
}
}
vm.drag = function() {
alert("you dragged something!");
};
The alert is not firing