【发布时间】:2022-01-25 16:51:48
【问题描述】:
$interval 服务和 Angualr.js 中的路由有问题 我想浏览抛出页面,我不想丢失另一页上的数据......我会解释......我正在使用库 chart.js 生成一个简单的折线图。 然后我每 5 秒生成一些数据,并将这些数据推送到一个数组中,以帮助我构建图表(如果你知道库你知道我在说什么)。当我路由到另一个页面并且回来时图表消失了,数据都消失了,它从零开始。我能做什么? 这是我的代码...
$(function(){
var myWallet = 1000000000;
var BTCWallet = 0;
var ETHWallet = 0;
var CROWallet = 0;
var app = angular.module("myApp", ["ngRoute","chart.js"]);
/**roting dell'app */
app.config(function($routeProvider,$locationProvider) {
$locationProvider.hashPrefix('');
$routeProvider
.when("/dashboard", {
templateUrl : "dashboard.html"
})
.when("/wallet", {
templateUrl : "wallet.html"
})
.when("/trading", {
templateUrl : "trading.html"
})
.when("/home", {
templateUrl : "home.html"
})
;
});
/**controller per il timer nella dashboard
*/
app.controller('countCtrl', function($scope, $interval){
$scope.theTimer = 5;
var stopTimer = $interval(()=>{
$scope.theTimer = $scope.theTimer -1;
if ($scope.theTimer == 0){
$scope.theTimer = $scope.theTimer +5;
}
},1000);
$scope.$on('$destroy', function() {
$interval.cancel(stopTimer);
});
});
/**
* Chat setup and options
*/
app.controller("chartCtrl", function($rootScope,$scope,$interval){
$scope.labels = [];
$scope.series = ['BTC', 'ETH' , 'CRO'];
$scope.data = [
[],[],[]
];
var stopChart = $interval(()=>{
console.log($scope.data[0].push($rootScope.BTCCoin *1));
console.log($scope.data[0]);
$scope.data[1].push($rootScope.ETHCoin *1);
$scope.data[1];
$scope.data[2].push($rootScope.CROCoin *1);
$scope.data[2];
$scope.ora = new Date();
var orario = $scope.ora.toLocaleTimeString();
console.log($scope.labels.push(orario));
console.log($scope.labels);
},5000);
$scope.$on('$destroy', function() {
$interval.cancel(stopChart);
});
});
/**currecy controller */
app.controller('currencyCtrl', function($scope,$rootScope, $interval){
$rootScope.BTCCoin = (Math.random()* 53000).toFixed(2); /* toFixed per scegliere quanti numeri generare dopo la virgola*/
$rootScope.ETHCoin = (Math.random()* 4500).toFixed(2);
$rootScope.CROCoin = (Math.random()* 49777).toFixed(2);
var stopCurrency = $interval(()=>{
do{
$rootScope.BTCCoin = (Math.random()* 53000).toFixed(2);
}while($rootScope.BTCCoin < 48000);
do{
$rootScope.CROCoin = (Math.random()* 49777).toFixed(2);
}while($rootScope.CROCoin < 3200);
do{
$rootScope.ETHCoin = (Math.random()* 4500).toFixed(2);
}while($rootScope.ETHCoin < 3800);
},5000);
$scope.$on('$destroy', function() {
$interval.cancel(stopCurrency);
});
})
});
【问题讨论】: