gpt4 book ai didi

javascript - 强制 AngularJS 在 html 端重新加载更新的值

转载 作者:行者123 更新时间:2023-11-30 21:20:52 24 4
gpt4 key购买 nike

我有一个计时器,这是我在 AngularJS 中编写的第一个应用程序。我一直在尝试自学 Angular ,但我相信我的模型设置方式可能存在根本性的脱节。虽然我可以通过 console.log 打印输出在 Web 控制台中看到更新的值,但 html 端的计时器似乎没有更新。

var myApp = angular.module('countdownTimer', []);


myApp.service('timerService',['$http','$timeout', function($http, $timeout){
var time = 180;
var self = this;

this.getPrettyTime = function(){
var minutes = time/60;
var seconds = time - minutes * 60;
if (seconds < 10){
myDateObject.date = minutes + ":0" + seconds;
}
else{
myDateObject.date = minutes + ":" + seconds;
}
return myDateObject;
}

var myDateObject = {
date: null
}
var onTimeout = function() {
time = time - 1;
if (time > 0){
console.log(time);
mytimeout = $timeout(onTimeout, 1000);

}
else{
time = 180;
mytimeout = $timeout(onTimeout, 1000);
}
}
this.start = function() {

$timeout(onTimeout, 1000);
}


}]);

myApp.controller('CounterController', ['$timeout','$scope', 'timerService', function($timeout, $scope, timerService){
/**$scope.counter = 180;
**/
//var date = new Date(null);
//date.setSeconds(timerService.getTime());
$scope.myDateObject = timerService.getPrettyTime();


$scope.start = function(){
timerService.start();

}

$scope.stop = function(){
$timeout.cancel(mytimeout);
}


}]);
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Example </title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<script src="app2.js"></script>
</head>

<body data-ng-app="countdownTimer">
<div data-ng-controller="CounterController">
{{myDateObject.date}}
<button data-ng-click="stop()">Stop</button>
<button data-ng-click="start()">Start</button>
</div>
</body>
</html>

如您在上面的示例中所见,网页上的计时器未更新。我的逻辑是否存在脱节(我是 Angular 的新手,所以我很想知道我哪里出错了)或者我是否完全误解了 Angular 功能?

谢谢

最佳答案

首先,在 getPrettyTime 中你需要做:

var minutes = Math.floor(time/60);

否则你会得到一个 float 。

之后,每次更新时间时都需要调用 getPrettyTime 函数,因此您可以在 onTimeout 函数中执行此操作,如下所示:

var onTimeout = function() {
time = time - 1;
self.getPrettyTime();
...

这是一个工作片段:

var myApp = angular.module('countdownTimer', []);


myApp.service('timerService', ['$http', '$timeout', function($http, $timeout) {
var time = 180;
var self = this;

self.getPrettyTime = function() {
var minutes = Math.floor(time / 60);
var seconds = time - minutes * 60;
if (seconds < 10) {
myDateObject.date = minutes + ":0" + seconds;
} else {
myDateObject.date = minutes + ":" + seconds;
}
return myDateObject;
}

var myDateObject = {
date: null
}
var onTimeout = function() {
time = time - 1;
self.getPrettyTime();
if (time > 0) {
console.log(time);
mytimeout = $timeout(onTimeout, 1000);

} else {
time = 180;
mytimeout = $timeout(onTimeout, 1000);
}
}
this.start = function() {

$timeout(onTimeout, 1000);
}


}]);

myApp.controller('CounterController', ['$timeout', '$scope', 'timerService', function($timeout, $scope, timerService) {
/**$scope.counter = 180;
**/
//var date = new Date(null);
//date.setSeconds(timerService.getTime());
$scope.myDateObject = timerService.getPrettyTime();


$scope.start = function() {
timerService.start();

}

$scope.stop = function() {
$timeout.cancel(mytimeout);
}


}]);
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title> Example </title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<script src="app2.js"></script>
</head>

<body data-ng-app="countdownTimer">
<div data-ng-controller="CounterController">
{{myDateObject.date}}
<button data-ng-click="stop()">Stop</button>
<button data-ng-click="start()">Start</button>
</div>
</body>

</html>

关于javascript - 强制 AngularJS 在 html 端重新加载更新的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45192710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com