Reputation: 17553
I have a custom directive that needs to watch for a mousemove
event on the element on which it is defined. Is it possible to use ng-mousemove
in this case, but have the function passed to ng-mousemove
refer to a method on my custom directive's scope. For example:
HTML
<div my-directive ng-mousemove="go()"></div>
Custom directive:
...
scope: true,
link: function(){
$scope.go = function () { ... };
}
I realize I can create the event listener within my directive to watch for a mousemove
event, but that seems to go against the standard Angular approach.
Upvotes: 0
Views: 88
Reputation: 222498
Relying on arbitrary function from directive's scope doesn't look like 'totally encapsulated', more like the opposite of it. And btw, you can't do that anyway.
Standard Angular approach is something like this
app.directive('myDirective', function () {
return {
scope: true,
transclude: true,
template: '<div ng-transclude ng-mousemove="go()"></div>',
link: function (scope){
scope.go = function () { ... };
}
};
});
Upvotes: 1