0

我试图在移动 Safari 的屏幕上显示我从模板函数返回的结果。我可以看到我的设备支持 devicemotion 我可以看到我的 vartiltFB 的控制台日志和数据

我看不到或无法工作的是它在我的移动 safari 上的 html 屏幕上显示。

Template.tapapp.showAngle = function() {

var tiltFB;

if (window.DeviceMotionEvent) {
  window.addEventListener('devicemotion', deviceMotionHandler, false);
  console.log("Devicemotion IS supported on your device.");
} else {

  console.log("Devicemotion Not supported on your device.");
  return "Devicemotion Not supported on your device.";
}

function deviceMotionHandler(eventData) {

  var acceleration = eventData.accelerationIncludingGravity;
  var rawAcceleration = "[" +  Math.round(acceleration.x) + ", " +     Math.round(acceleration.y) + ", " + Math.round(acceleration.z) + "]";

  var facingUp = -1;
  if (acceleration.z > 0) {
    facingUp = +1;
  }

  tiltFB = Math.round(((acceleration.y + 9.81) / 9.81) * 90 * facingUp);

  console.log(tiltFB);

  //Meteor.defer(function () {
  //     $('#angel').html(tiltFB);
  //     return tiltFB;
  });
  return tiltFB;
}

};

这是我的html:

<template name="tapapp">
  <div class="container">
  <h3 class="angel-class" id="angle">{{showAngle}}</h3>
</template>
4

1 回答 1

1

showAngle试试这个,你需要在你的助手中返回一个值,并通过 Session.set/get 反应性地连接它

客户端js

function deviceMotionHandler(eventData) {

  var acceleration = eventData.accelerationIncludingGravity;
  var rawAcceleration = "[" +  Math.round(acceleration.x) + ", " +     Math.round(acceleration.y) + ", " + Math.round(acceleration.z) + "]";

  var facingUp = -1;
  if (acceleration.z > 0) {
    facingUp = +1;
  }

  tiltFB = Math.round(((acceleration.y + 9.81) / 9.81) * 90 * facingUp);

  console.log(tiltFB);

  //Meteor.defer(function () {
  //     $('#angel').html(tiltFB);
  //     return tiltFB;
  //});
  Session.set("tiltFB", tiltFB);
}


Template.tapapp.showAngle = function() {
    return Session.get("tiltFB");
}

Meteor.startup(function() {
    if (window.DeviceMotionEvent) {
        window.addEventListener('devicemotion', deviceMotionHandler, false);
        console.log("Devicemotion IS supported on your device.");
    } else {
        console.log("Devicemotion Not supported on your device.");
    }
});
于 2013-02-21T22:02:59.957 回答