本文介绍了如何在 Angular.js 中使用hammer.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Angular.js 的新手,阅读我知道 angular 没有点击、双击等事件.我试图与 Hammer.js 结合但没有成功.

I'm new with Angular.js and reading i know angular dont have events like tap, double tap, etc. I'm trying to combine with Hammer.js without success.

来自 gist

/**
 * 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 文件中

now i've added to my html file

<!--...-->
<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 中使用它时它不起作用.

But when i try to use in my html it doesn't work.

我试过

<li hmTap="click($event)">...</li>
<li ngTap="click($event)">...</li>
<li hm-tap="click($event)">...</li>
<li ng-tap="click($event)">...</li>

没人工作,能帮我解决这个麻烦吗?

no one works, can help me with this trouble?

推荐答案

试试这个.

将hammer.js 和jquery.hammer.js 添加到您的解决方案中.使用 JQuery 和 Angular.

Add hammer.js and jquery.hammer.js to your solution. With JQuery and 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

In this example I am creating a new attribute called 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 事件创建一个指令:

You can then create a directive per Hammer event:

按住、点击、双击、transformstart、transform、transformend、dragstart、拖动、dragend、释放、滑动.

hold, tap, doubletap, transformstart, transform, transformend, dragstart, drag, dragend, release, swipe.

您的 HTML 将如下所示:

Your HTML would then look like this:

<button on-tap="DoAngularMethod()">Tap me</button>

这篇关于如何在 Angular.js 中使用hammer.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 21:44