Chcę wyświetlić dane odpowiedzi na wykresach. ale Tankvalue wychodzi z zakresu i przekazuje komunikat o błędzie

Tankvalue nie jest zdefiniowany.

Jak uzyskać dostęp do zmiennej na całym świecie. Wykresy nie ładują się z powodu niezdefiniowanej wartości. Chcesz pokazać Tankvalue na wykresie.

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
    $http.get('url', {
            headers: {
                'Authorization': 'Basic Pasword=='
            }
        })
        .then(function (response) {
            $scope.names = response.data;
            $scope.decodedFrame = atob($scope.names.dataFrame);
            var Tankvalue = $scope.decodedFrame.substring(6);


        });
});
FusionCharts.ready(function () {
    var fusioncharts = new FusionCharts({
        type: 'cylinder',
        dataFormat: 'json',
        id: 'fuelMeter-1',
        renderAt: 'chart-container',
        width: '200',
        height: '350',
        dataSource: {
            "chart": {
                "theme": "fint",
                "caption": "Tank",
                "subcaption": "Bakersfield Central",
                "lowerLimit": "0",
                "upperLimit": "25",
                "lowerLimitDisplay": "Empty",
                "upperLimitDisplay": "Full",
                "numberSuffix": " ltrs",
                "showValue": "1",
                "chartBottomMargin": "45",
                "showValue": "0"
            },
            "value": Tankvalue,
            "annotations": {
                "origw": "400",
                "origh": "190",
                "autoscale": "1",
                "groups": [{
                    "id": "range",
                    "items": [{
                        "id": "rangeBg",
                        "type": "rectangle",
                        "x": "$canvasCenterX-45",
                        "y": "$chartEndY-30",
                        "tox": "$canvasCenterX +45",
                        "toy": "$chartEndY-75",
                        "fillcolor": "#6caa03"
                    }, {
                        "id": "rangeText",
                        "type": "Text",
                        "fontSize": "11",
                        "fillcolor": "#333333",
                        "text": "80 ltrs",
                        "x": "$chartCenterX-45",
                        "y": "$chartEndY-50"
                    }]
                }]
            }

        },

    });
    fusioncharts.render();
});
0
pvkm 28 czerwiec 2017, 10:11

3 odpowiedzi

Najlepsza odpowiedź

$http.get() to funkcja asynchroniczna, więc FusionCharts.ready() uruchamia przed wartością przypisaną do zmiennej Tankvalue. Aby naprawić ten ruch {X3}} Funkcja wewnątrz .then().

Zrób to:

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
    $http.get('url', {
            headers: {
                'Authorization': 'Basic Pasword=='
            }
        })
      .then(function (response) {
        $scope.names = response.data;
        $scope.decodedFrame = atob($scope.names.dataFrame);
        var Tankvalue = $scope.decodedFrame.substring(6);
        FusionCharts.ready(function () {
        var fusioncharts = new FusionCharts({
        type: 'cylinder',
        dataFormat: 'json',
        id: 'fuelMeter-1',
        renderAt: 'chart-container',
        width: '200',
        height: '350',
        dataSource: {
            "chart": {
                "theme": "fint",
                "caption": "Tank",
                "subcaption": "Bakersfield Central",
                "lowerLimit": "0",
                "upperLimit": "25",
                "lowerLimitDisplay": "Empty",
                "upperLimitDisplay": "Full",
                "numberSuffix": " ltrs",
                "showValue": "1",
                "chartBottomMargin": "45",
                "showValue": "0"
            },
            "value": Tankvalue,
            "annotations": {
                "origw": "400",
                "origh": "190",
                "autoscale": "1",
                "groups": [{
                    "id": "range",
                    "items": [{
                        "id": "rangeBg",
                        "type": "rectangle",
                        "x": "$canvasCenterX-45",
                        "y": "$chartEndY-30",
                        "tox": "$canvasCenterX +45",
                        "toy": "$chartEndY-75",
                        "fillcolor": "#6caa03"
                    }, {
                        "id": "rangeText",
                        "type": "Text",
                        "fontSize": "11",
                        "fillcolor": "#333333",
                        "text": "80 ltrs",
                        "x": "$chartCenterX-45",
                        "y": "$chartEndY-50"
                    }]
                }]
            }

        },

    });
    fusioncharts.render();
   });   
  });
});
0
anoop 28 czerwiec 2017, 07:24

Nie można używać zmiennej lokalnej poza inną funkcją

$scope.Tankvalue = $scope.decodedFrame.substring(6);
0
Dixit 28 czerwiec 2017, 07:17
Move FusionCharts.ready() inside then block as shown below.
.then(function (response) {
            $scope.names = response.data;
            $scope.decodedFrame = atob($scope.names.dataFrame);
            var Tankvalue = $scope.decodedFrame.substring(6);

            FusionCharts.ready(function () {
                var fusioncharts = new FusionCharts({
                    type: 'cylinder',
                    dataFormat: 'json',
                    id: 'fuelMeter-1',
                    renderAt: 'chart-container',
                    width: '200',
                    height: '350',
                    dataSource: {
                        "chart": {
                            "theme": "fint",
                            "caption": "Tank",
                            "subcaption": "Bakersfield Central",
                            "lowerLimit": "0",
                            "upperLimit": "25",
                            "lowerLimitDisplay": "Empty",
                            "upperLimitDisplay": "Full",
                            "numberSuffix": " ltrs",
                            "showValue": "1",
                            "chartBottomMargin": "45",
                            "showValue": "0"
                        },
                        "value": Tankvalue,
                        "annotations": {
                            "origw": "400",
                            "origh": "190",
                            "autoscale": "1",
                            "groups": [{
                                "id": "range",
                                "items": [{
                                    "id": "rangeBg",
                                    "type": "rectangle",
                                    "x": "$canvasCenterX-45",
                                    "y": "$chartEndY-30",
                                    "tox": "$canvasCenterX +45",
                                    "toy": "$chartEndY-75",
                                    "fillcolor": "#6caa03"
                                }, {
                                    "id": "rangeText",
                                    "type": "Text",
                                    "fontSize": "11",
                                    "fillcolor": "#333333",
                                    "text": "80 ltrs",
                                    "x": "$chartCenterX-45",
                                    "y": "$chartEndY-50"
                                }]
                            }]
                        }

                    },

                });
                fusioncharts.render();
            });

        });
1
Deepthi S 28 czerwiec 2017, 07:24