问题描述
minesweeper.directive(’ngRightClick’, function($parse) { return function(scope, element, attrs) {var fn = $parse(attrs.ngRightClick); element.bind(’contextmenu’, function(event) {scope.$apply(function() { event.preventDefault(); fn(scope, {$event:event});});}); };});
如上代码,解释说是自定义ngRightClick指令,但是首先不能理解它的意思,再一个自定义指令不应该是下面这种形式吗:
minesweeper.directive('mineGrid',function(){ return {restrict:’E’,replace:false,templateUrl:’./templates/mineGrid.html’ }});
求指教
问题解答
回答1:首先自定义右键不是自定义元素,不应约束为element,更不会有HTML。直接返回function是直接返回link function的简写,自定义右键应该约束为属性。
回答2:这是使用了angular的内建服务$parse,建议看看这篇文章AngularJS内幕详解之 Directive