我是Angular.js的新手,并且阅读我知道Angular没有诸如tap,doubletap等事件。我试图与Hammer.js结合使用而没有成功。
要点代码
/** * angular-hammer.js * Inspired by AngularJS' implementation of "click dblclick mousedown..." * * This ties in the Hammer events to attributes like: * * hm-tap="add_something()" * hm-swipe="remove_something()" * * and also has support for Hammer options with: * * hm-tap-opts="{hold: false}" * * or any other of the "hm-event" listed underneath. */ angular.forEach('hmTap:tap hmDoubletap:doubletap hmHold:hold hmTransformstart:transformstart hmTransform:transform hmTransforend:transformend hmDragstart:dragstart hmDrag:drag hmDragend:dragend hmSwipe:swipe hmRelease:release'.split(' '), function(name) { var directive = name.split(':'); var directiveName = directive[0]; var eventName = directive[1]; angular.module('MYMODULE').directive(directiveName, ['$parse', function($parse) { return function(scope, element, attr) { var fn = $parse(attr[directiveName]); var opts = $parse(attr[directiveName + 'Opts'])(scope, {}); element.hammer(opts).bind(eventName, function(event) { scope.$apply(function() { console.log("Doing stuff", event); fn(scope, {$event: event}); }); }); }; }]); });
现在我已经添加到我的html文件中
<!--...--> <script src="/js/jquery-1.9.0.js"></script> <script src="/js/angular.js"></script> <script src="/js/hammer.js"></script> <script src="/js/jquery.hammer.js"></script> <script src="/js/angular-hammer.js"></script> <script src="/js/app/controllers.js"></script> </html>
但是,当我尝试在我的html中使用它时,它不起作用。
我尝试过
<li hmTap="click($event)">...</li> <li ngTap="click($event)">...</li> <li hm-tap="click($event)">...</li> <li ng-tap="click($event)">...</li>
没有人可以解决这个麻烦吗?
快去
将Hammer.js和jquery.hammer.js添加到您的解决方案中。使用JQuery和Angular。
像这样:
<script src="js/jquery/jquery.min.js"></script> <script src="js/hammer/hammer.js"></script> <script src="js/hammer/jquery.hammer.js"></script> <script src="js/angular/angular.min.js"></script>
然后使用Angular指令定义新的HTML属性。
在此示例中,我将创建一个名为 on-tap
on-tap
angular.module('myApp.directives', []) .directive('onTap', function () { return function (scope, element, attrs) { return $(element).hammer({ prevent_default: false, drag_vertical: false }) .bind("tap", function (ev) { return scope.$apply(attrs['onTap']); }); }; });
然后,您可以为每个Hammer事件创建一个指令:
按住,点击,双击,transformstart,transform,transformend,dragstart,拖动,dragend,释放,滑动。
您的HTML将如下所示:
<button on-tap="DoAngularMethod()">Tap me</button>