[英]angularjs services and controllers data out of sync
I've got what I thought was a fairly simple AngularJS application.我已经得到了我认为是一个相当简单的 AngularJS 应用程序。 I used to have a simple countdown timer in my controller code, but I decided to break it out into its own service.我曾经在我的控制器代码中有一个简单的倒数计时器,但我决定将其分解为自己的服务。 That's where the problems began.这就是问题开始的地方。
Previously, when my timer code was embedded within the controller, the countdown
scope variable displayed correctly - every second, it would count down one less, until 0, as per the timer function.以前,当我的计时器代码嵌入到控制器中时, countdown
范围变量会正确显示 - 每一秒,它都会根据计时器功能倒计时 1,直到 0。 However, now that I've moved this to a service, and been passing the data back and forth with some function calls, the countdown
variable counts down 2 numbers every second, rather than 1. If I console.log(countdown);
但是,现在我已经将它移到了一个服务中,并且通过一些函数调用来回传递数据, countdown
变量每秒countdown
2 个数字,而不是 1 个。如果我console.log(countdown);
in my service's rundownClock()
function, the correct countdown number is displayed each pass, however: 10,9,8,7...to 1.在我的服务的rundownClock()
函数中,每次通过都会显示正确的倒计时数,但是:10,9,8,7...to 1。
Can anyone figure out what I'm now doing wrong, and why this "double counting" is occurring?谁能弄清楚我现在做错了什么,以及为什么会发生这种“重复计算”? Am I not maintaining the scope correctly in the controller?我没有在控制器中正确维护范围吗?
Here is some of the controller, with the relevant CountdownService bits highlighted:这是一些控制器,突出显示了相关的 CountdownService 位:
myApp.controller('myCtrl', ['$scope', 'CountdownService', function($scope, CountdownService) {
// TIMER SERVICES
$scope.startTimer = CountdownService.startTimer;
$scope.runClock = function () {
$scope.updateCountdown();
if (($scope.countdown > 0) && ($scope.roundStarted == true)) {
CountdownService.rundownClock($scope.countdown);
}
};
$interval($scope.runClock, 1000);
$scope.updateCountdown = function () {
CountdownService.setCurrentRound($scope.currentRound);
$scope.countdown = CountdownService.getCountdown();
$scope.roundStarted = CountdownService.getRoundStarted();
}
}]);
Here's some of the service in question.这是一些有问题的服务。 (Don't worry about the rounds variable set-up at the beginning, it's not relevant to the problem): (不要担心开始时的 rounds 变量设置,它与问题无关):
myApp
.factory("CountdownService", function (gameSetUp) {
var rounds = gameSetUp.rounds,
roundStarted = false,
roundFinished = false,
currentRound = 0,
countdown = rounds[currentRound].time;
// grab the round from the controller's scope to set the current round
function setCurrentRound(round) {
currentRound = round;
}
function getRoundStarted() {
return roundStarted;
}
function getCountdown() {
return countdown;
}
function startTimer() {
roundStarted = true;
}
function rundownClock() {
if (roundStarted === true) {
if (countdown > 0) {
countdown = countdown - 1;
}
if (countdown === 0) {
roundFinished = true;
}
}
}
return {
startTimer: startTimer,
rundownClock: rundownClock,
getCountdown: getCountdown,
getRoundStarted: getRoundStarted,
setCurrentRound: setCurrentRound
};
});
And finally, a snippet from the view, where the countdown scope variable is displayed:最后,视图中的一个片段,其中显示了倒计时范围变量:
<div class="timer md-body-2">{{ countdown }} seconds</div>
Update @downvoter :更新@downvoter:
Here is a working demo ( without using controller in 2 places route and template)这是一个工作演示(在 2 个地方路由和模板中不使用控制器)
Here is the exact behavior that the author is talking about ( using controller in route and template )这是作者正在谈论的确切行为(在路由和模板中使用控制器)
My original answer我的原答案
I think your myCtrl
controller is running twice, so, your $interval($scope.runClock, 1000);
我认为你的myCtrl
控制器运行了两次,所以,你的$interval($scope.runClock, 1000);
is running twice also ...也在运行两次......
Are using registering myCtrl
as route controller and in your template with ng-controller ?是否使用将myCtrl
注册为路由控制器并在您的模板中使用 ng-controller ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.