【问题标题】:Touch in Angular在 Angular 中触摸
【发布时间】:2015-12-17 21:38:52
【问题描述】:

这是我从触摸事件中获得的返回数据。明显缺少任何有用的触摸 X/Y 坐标 (https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches)。这是一个无可救药的通用问题,但是,有什么想法吗?传递给在触摸时执行的函数的“事件”对象:

{
      "originalEvent": {
        "isTrusted": true
      },
      "type": "touchstart",
      "timeStamp": 1450388006795,
      "jQuery203026962137850932777": true,
      "which": 0,
      "view": "$WINDOW",
      "target": {},
      "shiftKey": false,
      "metaKey": false,
      "eventPhase": 3,
      "currentTarget": {},
      "ctrlKey": false,
      "cancelable": true,
      "bubbles": true,
      "altKey": false,
      "delegateTarget": {},
      "handleObj": {
        "type": "touchstart",
        "origType": "touchstart",
        "data": null,
        "guid": 2026,
        "namespace": ""
      },
      "data": null
    }

现在,这是在画布的角度 UI 模式中,但鼠标事件可以正常工作。这是我的元素顺便说一句:

link: function(scope, element, attrs, model){
                //scope.canvasElem = element[0].children[0].children[0];
                scope.canvasElem = angular.element($('.touchScreen'))[0];
                scope.ctx = scope.canvasElem.getContext('2d');

这是我如何绑定的示例:

element.bind('touchstart', scope.touchStart);

编辑,这里有一个mousedown事件对象进行比较:

{
  "originalEvent": {
    "isTrusted": true
  },
  "type": "mousedown",
  "timeStamp": 1450389131400,
  "jQuery20309114612976554781": true,
  "toElement": {},
  "screenY": 436,
  "screenX": 726,
  "pageY": 375,
  "pageX": 726,
  "offsetY": 81,
  "offsetX": 41,
  "clientY": 375,
  "clientX": 726,
  "buttons": 1,
  "button": 0,
  "which": 1,
  "view": "$WINDOW",
  "target": {},
  "shiftKey": false,
  "relatedTarget": null,
  "metaKey": false,
  "eventPhase": 3,
  "currentTarget": {},
  "ctrlKey": false,
  "cancelable": true,
  "bubbles": true,
  "altKey": false,
  "delegateTarget": {},
  "handleObj": {
    "type": "mousedown",
    "origType": "mousedown",
    "data": null,
    "guid": 2025,
    "namespace": ""
  },
  "data": null
}

【问题讨论】:

    标签: javascript angularjs canvas touch


    【解决方案1】:

    看起来您正在使用 jQuery,或者至少是类似 jQuery 的实现。 jQuery 不会将所有触摸事件属性复制到规范化事件包装器。不过,您可以在 originalEvent 对象下找到它们。

    originalEvent 属性包含原生触摸事件,您可以根据规范使用。

    例如这样的代码:

    $('body').on('touchmove', function(e) {
        // Access the original event like this:
        console.log(e.originalEvent);
    });
    

    【讨论】:

    • 嘿,感谢您的回复,您能详细说明我是如何获得该包装的吗?
    • @VSO 我添加了一个如何将原始事件对象记录到控制台的示例。
    • 我试过了,它返回的只是:{“isTrusted”:true}。原始对象中的属性(在我的问题帖子中)是我可以访问的全部。我真的希望我错过了什么。
    • @VSO 你使用的是 jQuery 还是 jQuery 之类的东西?也许您需要的属性没有被控制台枚举,但如果您按名称访问它,它仍然存在。
    • Alexander,它可能是 jQuery lite 或任何预先打包好的 Angular。我会仔细看看的。再次感谢您的帮助,它缩小了范围。
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多