gpt4 book ai didi

javascript - 如何增加 Angular ng-repeat 函数值?

转载 作者:太空宇宙 更新时间:2023-11-04 15:43:01 25 4
gpt4 key购买 nike

我有一个 Angular 函数,它根据 Controller 中的函数在 DOM 中渲染,我需要在计时器中增加该函数。这可以做到吗?我还希望在一页上有多个独立工作的计时器。我该怎么办?

var app = angular.module('timer', []);

app.controller('MainCtrl', function($scope, $timeout) {
$scope.behaviors = {
"num": "1",
"minutes": "30",
"seconds": "15"
}, {
"num": "2",
"minutes": "10",
"seconds": "3"
}, {
"num": "3",
"minutes": "5",
"seconds": "43"
};
$scope.secondsX = function(behavior) {
return 0;
};
$scope.minutesX = function(behavior) {
return 0;
};
$scope.start = function(behavior) {
durationTimer();
};

function durationTimer() {
dtimeoutId = $timeout(function() {
dupdateTime();
durationTimer();
}, 1000);
}

function dupdateTime() {
$scope.secondsX = function() {
return $scope.seconds++;
}
if ($scope.seconds === 60) {
$scope.secondsX = function() {
return 0;
}
$scope.minutesX = function() {
return $scope.minutes++;
}
}
}

});
<!DOCTYPE html>
<html ng-app="timer">

<head>
<meta charset="utf-8" />
<title>Timer</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@1.0.x" src="https://code.angularjs.org/latest/angular.min.js" data-semver="4.0.0"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<div ng-repeat="behavior in behaviors">
<span ng-model="minutes">{{minutesX(behavior)}}</span>
<span ng-model="seconds">{{secondsX(behavior)}}</span>
<button ng-click="start(behavior)">Start</button>
</div>
</body>

</html>

最佳答案

我刚刚更正了代码,它似乎按您的预期工作。 Working code .

您需要将每个循环下的 Controller 范围分开。请引用行<div ng-controller="MainCtrl" ng-repeat="behavior in behaviors"> .

<!DOCTYPE html>
<html ng-app="timer">

<head>
<meta charset="utf-8" />
<title>Timer</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@1.0.x" src="https://code.angularjs.org/latest/angular.min.js" data-semver="4.0.0"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<div ng-controller="MainCtrl" ng-repeat="behavior in behaviors">
<span ng-model="minutes">{{minutesX(behavior.minutes)}}</span>
<span ng-model="seconds">{{secondsX(behavior.seconds)}}</span>
<button ng-click="start(behavior)">Start</button>
</div>

<script>
var app = angular.module('timer', []);

app.controller('MainCtrl', function($scope, $timeout) {
$scope.behaviors = [{
"num": "1",
"minutes": "30",
"seconds": "15"
}, {
"num": "2",
"minutes": "10",
"seconds": "3"
}, {
"num": "3",
"minutes": "5",
"seconds": "43"
}];

$scope.secondsX = function(behavior) {
return behavior = 0;
};
$scope.minutesX = function(behavior) {
return behavior = 0;
};
$scope.seconds = 0;
$scope.minutes = 0;
$scope.start = function(behavior) {
//console.log('timer start ='+behavior.seconds);
durationTimer(behavior);
};
function durationTimer(behavior) {
dtimeoutId = $timeout(function() {
$scope.seconds++;
if ($scope.seconds === 60) {
$scope.seconds = 0
$scope.minutes++;
}
dupdateTime(behavior);
durationTimer(behavior);
}, 1000);
}

function dupdateTime(behavior) {
console.log('current time = '+$scope.seconds)
$scope.secondsX = function() {
return behavior.seconds = $scope.seconds;
}
$scope.minutesX = function() {
return behavior.minute = $scope.minutes;
}
}

});
</script>

</body>
</html>

关于javascript - 如何增加 Angular ng-repeat 函数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43729486/

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