如何跟踪离子应用程序中页面上的所有点击?

时间:2015-06-26 20:42:56

标签: angularjs cordova ionic-framework angularjs-directive

我创建了包含复选框的简单页面。在此页面上,用户可以多次检查和取消选中复选框。我想跟踪所有这些事件?我怎么能这样做?

这是我的代码。

app.js

var pmApp = angular.module('pmApp', ['ionic']);

pmApp.controller('CheckboxController', function($scope) {
  $scope.devList = [
    { text: "Device & app history", details : "Allows the app to view one or more of: information about activity on the device, which apps are running, browsing history and bookmarks" ,checked: true },
    { text: "Identity", details: "Uses one or more of: accounts on the device, profile data", checked: false },
    { text: "Calendar", details: "Uses calendar information", checked: false },
    { text: "Contact", details: "Uses contact information", checked: false },
    { text: "Location", details: "Uses the device's location", checked: false },
    { text: "SMS", details: "Uses one or more of: SMS, MMS. Charges may apply.", checked: false }
  ];

  $scope.selection=[];
  // toggle selection for a given employee by name
  $scope.toggleSelection = function toggleSelection(item) {
     var idx = $scope.selection.indexOf(item);

     // is currently selected
     if (idx > -1) {
       $scope.selection.splice(idx, 1);
     }

     // is newly selected
     else {
       $scope.selection.push(item);
     }
   };

});

的index.html

    <div class="list" ng-controller="CheckboxController">
        <ion-checkbox ng-repeat="item in devList"
                      ng-model="item.checked" 
                      ng-checked="selection.indexOf(item) > -1"
                      ng-click="toggleSelection(item)"
                      >
            {{ item.text }}
            <h3 class="item-text-wrap"> {{ item.details }}</h3>
        </ion-checkbox>
<div class="item">
          <pre ng-bind="selection | json"></pre> 
</div>
      </div>

在此先感谢,任何帮助将不胜感激。

此致

2 个答案:

答案 0 :(得分:1)

您可以使用https://docs.angularjs.org/api/ng/directive/ngMouseover为所有元素制作鼠标悬停计数器:然后使用 https://docs.angularjs.org/api/ng/directive/ngClick记录点击次数,https://docs.angularjs.org/api/ng/directive/ngMousemove记录要移动的鼠标并获取位置:

使用的一切:     NG-点击     NG-DBLCLICK     NG-鼠标按下     NG-鼠标松开     NG-的mouseenter     NG-鼠标离开     NG-鼠标移动     NG-鼠标悬停

以下是一些示例代码:

HTML:                                  

<body ng-app="mainModule">
  <div ng-controller="mainController">
    <h3>1. Click</h3>
    <button id="firstBtn" ng-click="onFirstBtnClick()">Click me</button>
    <strong>RESULT:</strong> {{onFirstBtnClickResult}}<br />
    <br />
    <h3>2. Click with Dependency Injection</h3>
    <label>Type something: <input type="text" ng-model="secondBtnInput"></label>
    <button id="secondBtn" ng-click="onSecondBtnClick(secondBtnInput)">Click me</button><br />
    <strong>RESULT:</strong> {{onSecondBtnClickResult}}<br />
    <br />
    <h3>3. Double click</h3>
    Double-click the square<br />
    <img src="images/square.png" ng-dblclick="onDblClick()" /><br />
    <strong>RESULT:</strong> {{onDblClickResult}}<br />
    <h3>4. Mouse down, up, enter, leave, move, over</h3>
    Move the mouse on the square<br />
    <img src="images/square.png"
         ng-mousedown="onMouseDown($event)"
         ng-mouseup="onMouseUp($event)"
         ng-mouseenter="onMouseEnter($event)"
         ng-mouseleave="onMouseLeave($event)"
         ng-mousemove="onMouseMove($event)"
         ng-mouseover="onMouseOver($event)" /><br />
    <strong>MOUSE DOWN RESULT:</strong> {{onMouseDownResult}}<br />
    <strong>MOUSE UP RESULT:</strong> {{onMouseUpResult}}<br />
    <strong>MOUSE ENTER RESULT:</strong> {{onMouseEnterResult}}<br />
    <strong>MOUSE LEAVE RESULT:</strong> {{onMouseLeaveResult}}<br />
    <strong>MOUSE MOVE RESULT:</strong> {{onMouseMoveResult}}<br />
    <strong>MOUSE OVER RESULT:</strong> {{onMouseOverResult}}
  </div>
</body>
</html>

JS

angular.module("mainModule", [])
  .controller("mainController", function ($scope)
  {
    // Initialization
    $scope.onFirstBtnClickResult = "";
    $scope.secondBtnInput = "";
    $scope.onDblClickResult = "";
    $scope.onMouseDownResult = "";
    $scope.onMouseUpResult = "";
    $scope.onMouseEnterResult = "";
    $scope.onMouseLeaveResult = "";
    $scope.onMouseMoveResult = "";
    $scope.onMouseOverResult = "";

    // Utility functions

    // Accepts a MouseEvent as input and returns the x and y
    // coordinates relative to the target element.
    var getCrossBrowserElementCoords = function (mouseEvent)
    {
      var result = {
        x: 0,
        y: 0
      };

      if (!mouseEvent)
      {
        mouseEvent = window.event;
      }

      if (mouseEvent.pageX || mouseEvent.pageY)
      {
        result.x = mouseEvent.pageX;
        result.y = mouseEvent.pageY;
      }
      else if (mouseEvent.clientX || mouseEvent.clientY)
      {
        result.x = mouseEvent.clientX + document.body.scrollLeft +
          document.documentElement.scrollLeft;
        result.y = mouseEvent.clientY + document.body.scrollTop +
          document.documentElement.scrollTop;
      }

      if (mouseEvent.target)
      {
        var offEl = mouseEvent.target;
        var offX = 0;
        var offY = 0;

        if (typeof(offEl.offsetParent) != "undefined")
        {
          while (offEl)
          {
            offX += offEl.offsetLeft;
            offY += offEl.offsetTop;

            offEl = offEl.offsetParent;
          }
        }
        else
        {
          offX = offEl.x;
          offY = offEl.y;
        }

        result.x -= offX;
        result.y -= offY;
      }

      return result;
    };

    var getMouseEventResult = function (mouseEvent, mouseEventDesc)
    {
      var coords = getCrossBrowserElementCoords(mouseEvent);
      return mouseEventDesc + " at (" + coords.x + ", " + coords.y + ")";
    };

    // Event handlers
    $scope.onFirstBtnClick = function () {
      $scope.onFirstBtnClickResult = "CLICKED";
    };

    $scope.onSecondBtnClick = function (value) {
      $scope.onSecondBtnClickResult = "you typed '" + value + "'";
    };

    $scope.onDblClick = function () {
      $scope.onDblClickResult = "DOUBLE-CLICKED";
    };

    $scope.onMouseDown = function ($event) {
      $scope.onMouseDownResult = getMouseEventResult($event, "Mouse down");
    };

    $scope.onMouseUp = function ($event) {
      $scope.onMouseUpResult = getMouseEventResult($event, "Mouse up");
    };

    $scope.onMouseEnter = function ($event) {
      $scope.onMouseEnterResult = getMouseEventResult($event, "Mouse enter");
    };

    $scope.onMouseLeave = function ($event) {
      $scope.onMouseLeaveResult = getMouseEventResult($event, "Mouse leave");
    };

    $scope.onMouseMove = function ($event) {
      $scope.onMouseMoveResult = getMouseEventResult($event, "Mouse move");
    };

    $scope.onMouseOver = function ($event) {
      $scope.onMouseOverResult = getMouseEventResult($event, "Mouse over");
    };
  });

答案 1 :(得分:0)

如果符合您的需求,您可以尝试使用最新的Ionic Analytics。有关其官方博客文章的更多信息:http://docs.ionic.io/v1.0/docs/analytics-from-scratch

用法很简单(来自additional docs):

.controller('MyCtrl', function($ionicAnalytics) {

  $ionicAnalytics.track('Purchase Item', {
     item_id: 'lpdsx,
     item_name: 'Leopard Socks'
   }); 
});