简体   繁体   English

当我更改angular.js中的视图时如何防止图表发生错误

[英]how to prevent an error occurring by a chart when I change my view in angular.js

I have two views. 我有两种看法。 view A and view B. in the view This is my chart. 视图A和视图B。在视图中这是我的图表。 When I go from view A to view BI quickly get an error. 当我从视图A转到视图BI时,迅速得到一个错误。 I suppose this error is received because there is a settimeout and when the view is changed the function is not finished yet. 我想收到此错误是因为存在settimeout,并且当更改视图时功能尚未完成。 How can I avoid having this error? 如何避免出现此错误?

/* myApp module */
var myApp = angular.module('myApp', ['ui.router'])
.config(function ($stateProvider) {

    $stateProvider.state('A', {
        url: "/A",
        template: '<div id="chart"></div><a ui-sref="B">NEXT</a>',
        controller: function ($scope) {
        var chartSensorial = c3.generate({
          bindto: '#chart',
            data: {
                columns: [
                    ['&nbsp;', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                ],
                type: 'bar',
                labels:true
            },
            axis: {
                rotated: true,
                x: {
                    type: 'category',
                    categories: ['α A.N', 'NUC', 'LYS', 'PHE', 'LEU', 'ILE', 'CYS', 'MET', 'VAL', 'TYR', 'PRO', 'ALA', 'THR', 'ARG', 'HIS', 'GLY', 'SER', 'GLU', 'ASP'],
                },
                y:{
                  label:"Concentración"
                }
            },
            transition: {
               duration: 2000
            }
        }); 

         setTimeout(function () {
            chartSensorial.load({
            columns: [
                    [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]
                ]
            });
          },2000)
        }
    }).state('B', {
        url: "/B",
        template: '<a ui-sref="A">BACK</a>',
        controller: function ($scope) {
            console.log("B");
        }
    });
})

.controller('MainCtrl', function ($scope, $state) {
 $state.go("A");
});

http://jsfiddle.net/yruj7n4e/ http://jsfiddle.net/yruj7n4e/

在此处输入图片说明

You should cancel your timeout when $scope is destroyed, this way you avoid acessing scope related stuff after scope is destroyed. $ scope被破坏时,您应该取消超时,这样可以避免在作用域被破坏之后再访问与作用域相关的东西。

To do that you should first save the return of setTimeout , listen to $destroy and clear your timeout there: 为此,您应该首先保存setTimeout的返回值,听$ destroy并清除那里的超时:

var myTimeout = setTimeout(function () {
    chartSensorial.load({
        columns: [
            [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]
        ]
    });
},2000);

$scope.$on('$destroy', function(){
    clearTimeout(myTimeout); //clear timeout so if it didn't trigger yet it will never be triggered
});

By the way, I suggest you check anguar's $timeout instead of using setTimeout/clearTimeout 顺便说一句,我建议您检查安瓜尔的$timeout而不是使用setTimeout / clearTimeout

Why you need to have transition duration & chartSensorial.load timeout to 2000? 为什么您需要将transition durationchartSensorial.load超时设置为2000? Because of that it's interrupting the chart loading when you change state in between. 因此,当您在两者之间更改状态时,它会中断图表加载。 If you change state after its complete load the error will not occur. 如果在完全加载后更改状态,则不会发生错误。

Or you can make those timeout small eg 100 (ms) each, so that it will load fast & you won't get those errors on console after changing states fast. 或者,您可以将这些超时时间设置得较小,例如每次100(ms),这样它可以快速加载,并且在快速更改状态后不会在控制台上收到这些错误。

Working jsFiddle 工作jsFiddle

PS Also in AngularJS app prefer using $timeout over setTimeout (so inside code will be angular's scope) PS同样在AngularJS应用中,更喜欢使用$ timeout而不是setTimeout(因此内部代码将是angular的范围)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM