如何处理$ interval不是angularjs中的函数错误?

时间:2017-01-18 15:12:17

标签: javascript angularjs

当我点击向上/向下箭头时,我正在使用angularjs $ interval函数来连续增加/减少一个值。但我得到一个错误" TypeError:$ interval不是一个函数"当我试图增加/减少时。如何解决这个问题,提前谢谢。这是我到目前为止编写的代码:



$scope.onMouseDown = function (type) {
  promise = $interval(function () {
    if (type == 'Inc') {
       $scope.increaseVal();
    }
    else if (type == 'Dec') {
       $scope.decreaseVal();
    }
  }, 150);
};
$scope.stopInterval = function () {
   $interval.cancel(promise);
};

<div><a href="javascript:;" class="arrow" data-spin="up"><i class="fa fa-caret-up" ng-mousedown="onMouseDown('Inc')" ng-mouseup="stopInterval()" ng-mouseleave="stopInterval()" ng-click="increaseVal()"></i></a>
<a href="javascript:;" class="arrow" data-spin="down"><i class="fa fa-caret-down" ng-mousedown="onMouseDown('Dec')"
ng-mouseup="stopInterval()" ng-mouseleave="stopInterval()" ng-click="decreaseVal()"></i></a>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

将$ interval服务注入您的控制器:

angular.module('yourmodulename').controller('yourcontrollername', [
    '$scope', '$interval',
    function ($scope, $interval) {
       $scope.onMouseDown = function (type) {
           promise = $interval(function () {
               if (type == 'Inc') {
                   $scope.increaseVal();
               }
               else if (type == 'Dec') {
                   $scope.decreaseVal();
               }
           }, 150);
       };
       $scope.stopInterval = function () {
           $interval.cancel(promise);
       };
}]);