我是 angularjs 的新手。我已经编写了这个幸运数字生成器应用程序,它在单击“生成”按钮后 5 秒后生成幸运数字。我想添加一个在 5 秒内完成的进度条,然后显示结果。当前进度条仅在页面加载时显示。如何避免这种情况?
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.show = true;
$scope.$timeout = $timeout;
$scope.generate = function() {
var k = Math.floor(Math.random() * 10 + 1);
if (k == $scope.userinput) {
$scope.result = "Hooray! You won $10000!";
} else {
$scope.result = "Lucky number generated: " + k + "Try again!";
}
};
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<style>
#progressbar {
height: 5px;
width: 100%;
background-color: red;
animation-name: prgb;
animation-duration: 5s;
}
@keyframes prgb {
0% {
width: 0%;
}
25% {
width: 25%;
}
50% {
width: 50%;
}
75% {
width: 75%;
}
100% {
width: 100%;
}
}
</style>
<div ng-app="myApp" ng-controller="myCtrl">
Enter number between 0-10: <input type="text" ng-model="userinput">
<button ng-click="$timeout(generate, 5000);">Generate</button><br><br>
<div id="progressbar" ng-show="show" ng-initialize="show=false;"></div>
<div>{{result}}</div>
</div>
</body>
</html>