请帮助修复脚本。 http://jsfiddle.net/k9r4t/
悬停时。 button_hover增加计数器的值。但是当光标离开时,计数器的值会显示在屏幕上。 button_hover。并且用户只能看到计数器的初始值和最终值。
我希望用户看到中间值
控制器代码:
var app = angular.module("moduleCounter", []);
app.controller("controllerCounter", function ($scope){
$scope.counter = 0;
$scope.incrementCounter = function(){
$scope.startNext = setInterval(function(){
$scope.counter++;
}, 400);
}
$scope.stopCounter = function(){
clearInterval($scope.startNext);
}
});
答案 0 :(得分:0)
使用$timeout
,它是Angular的本机服务:
var app = angular.module("moduleCounter", []);
app.controller("controllerCounter", function ($scope , $timeout){
$scope.counter = 0;
$scope.incrementCounter = function(){
$scope.setTimeout();
}
$scope.setTimeout = function(){
$scope.startNext = $timeout( $scope.counterUp , 400);
}
$scope.counterUp = function(){
$scope.counter++;
$scope.setTimeout();
}
$scope.stopCounter = function(){
$timeout.cancel($scope.startNext);
}
});
JSFiddle:http://jsfiddle.net/cherniv/k9r4t/1/