2015-04-03 57 views
0

我有一个创建指南针的离子ngCordova应用程序。它工作正常,但我呼吁clearWatch()不做任何事情。

我有两个按钮的模板:

<ion-view title="Compass Page"> 
    <ion-content> 
     <button class="button" ng-click="startCompass()">Start</button> 
     <button class="button" ng-click="stopCompass()">Stop</button><br> 
     {{dir}} 
    </ion-content> 
</ion-view> 

该控制器被如下:当我按下启动按钮和{{DIR}}值

angular.module('starter') 
    .controller('CompassCtrl', function($scope, $cordovaDeviceOrientation) { 

     $scope.dir = 0; 

     $scope.startCompass = function() { 
      var options = { 
       frequency: 500 
      }; 

      $scope.watchId = $cordovaDeviceOrientation.watchHeading(options) 
       .then(
        null, 
        function(error) { 
         console.log(error); 
        }, 
        function(result) { 
         $scope.dir = result.trueHeading; 
        } 
       ); 
     }; 

     $scope.stopCompass = function() { 
      $cordovaDeviceOrientation.clearWatch($scope.watchId); 
     }; 
    }); 

指南针启动细在我旋转设备时更新。我打电话给clearWatch什么都不做。

任何指针,我做错了将不胜感激。

艾丹

回答

0

如果有人能解释我会很感激,但我发现的是,

$scope.watchId = $cordovaDeviceOrientation.watchHeading(options).then(...); 

$scope.watchId = $cordovaDeviceOrientation.watchHeading(options); 

不同说完看着两个源代码cordova插件和ngCordova我发现从watchHeading存储承诺,然后添加then()子句修复了这个问题:

$scope.startCompass = function() { 
    var options = { 
     frequency: 500 
    }; 

    $scope.watchPromise = $cordovaDeviceOrientation.watchHeading(options); 

    $scope.watchPromise.then(
      null, 
      function(error) { 
       console.log(error); 
      }, 
      function(result) { 
       $scope.dir = result.trueHeading; 
      } 
     ); 

}; 

$scope.stopCompass = function() { 
    $cordovaDeviceOrientation.clearWatch($scope.watchPromise.watchID); 
};