Диаграмма угловых данных от другого javascript - PullRequest
0 голосов
/ 21 мая 2018

У меня есть массив в data_controller.js, и я хотел, чтобы мой main.js, где я там редактировал свою угловую диаграмму, мог получить массив.Есть какой-то конкретный способ сделать это?

Data_Controller.js:

/*global angular*/
var app = angular.module('statisticsApp', [chart.js]).controller('myCtrl',
function ($scope, $http) {
"use strict";
return $http({
    method : "POST",
    url : "GatewayAPI.php",

}).then(function mySuccess(response) {
    $scope.records = response.data;     
    var mydata,myJSON,myresult,myjava, myobj;
    var i;
    var Result; 
    var chartResultTemp = [];
    var chartResultph = [];
    var chartResultHum = [];
    var resultType = [];


    for(i=0; i<72;i++)
    {
        //storing data
        mydata = $scope.records.data[i];

        //retrieving data

        myobj = mydata.data.substring(3,4);
        resultType = mydata.data.substring(3, 4);

        if(resultType === "A") {
            chartResultTemp.push([mydata.data.substring(6,9)]);
        } else if (resultType ==="B") {
            chartResultph.push([mydata.data.substring(6, 9)]);   
        } else {
            chartResultHum.push([mydata.data.substring(6, 9)]);   
        };


        $scope.test=Result; //change to array
        $scope.test2=chartResultTemp;
        $scope.test3 = resultType;
        $scope.test4 = chartResultph;
        $scope.test5 = chartResultHum;

        console.log(Result);
        console.log(resultType);
    }




    $scope.gotTemp = false;
    $scope.gotHumidity = false;
    $scope.getSoilMoisture = false;

   });

});

main.js:

var app = angular.module("statisticsApp", ["chart.js"]);
app.controller("LineCtrl", function ($scope) {
"use strict";
$scope.labels = ["0200", "0400", "0600", "0800", "1000", "1200", "1400", 
"1600", "1800", "2000", "2200", "0000"];
$scope.series = ['Temperature (°C)'];
$scope.data = [
[26.5, 26.8, 26.3, 25.8, 29.4, 30.2, 31.5, 31.0, 28.4, 27.6, 26.3, 25.7]
];

$scope.onClick = function (points, evt) {
console.log(points, evt);
 };
});

Я попытался поместить функцию диаграммы из main, js в data_controller.js и написал $scope.data.push ([mydata.data.substring (6,9)]), но это ничего не дало.

Как мне вызвать функцию в data_controller.js и использовать массив в моем $ scope.data = [] в main.js?

1 Ответ

0 голосов
/ 21 мая 2018

Если вы хотите использовать этот метод для извлечения данных, было бы лучше разделить их на службу.Затем вы можете использовать все свои контроллеры.

Обратите внимание, что приведенный ниже код просто для демонстрации реализации, он может не сработать сразу, если вы просто скопируете и вставите его.

//statisticsSvc.js
angular.module('statisticsApp').service('statisticsService',['$http' function($http){
   var data = [];
   return {
     getStatistics: function(){
       return $http({
                method : "POST",
                url : "GatewayAPI.php",
              })
              .then(function(response){
                var mydata,myJSON,myresult,myjava, myobj;
                var i;
                var Result; 
                var chartResultTemp = [];
                var chartResultph = [];
                var chartResultHum = [];
                var resultType = [];


                for(i=0; i<72;i++)
                {
                  //storing data
                  mydata = response.data.data[i];

                  //retrieving data

                  myobj = mydata.data.substring(3,4);
                  resultType = mydata.data.substring(3, 4);

                  if(resultType === "A") {
                    chartResultTemp.push([mydata.data.substring(6,9)]);
                  } else if (resultType ==="B") {
                    chartResultph.push([mydata.data.substring(6, 9)]);   
                  } else {
                    chartResultHum.push([mydata.data.substring(6, 9)]);   
                  };

                  return {
                    records: response.data.data,
                    Result: Result,
                    chartResultTemp: chartResultTemp,
                    resultType: resultType,
                    chartResultph: chartResultph,
                    chartResultHum: chartResultHum
                  };
                }
              })
              .catch(function(error){
                console.log(error);
                return error;
              })
     },
              setData: function(a){ data = a;},
              getData: function(){ return data;} 
   };
}]);

Затем вы можете использовать эту услугу в ваших контроллерах:

    //myCtrl
    var app = angular.module('statisticsApp', [chart.js]).controller('myCtrl',
    function ($scope, $http,statisticsService) {

    //your call to service
    statisticsService.getStatistics().then(function(response){
      $scope.records = response.records;
      $scope.test = response.Result;
      $scope.test2 = response.chartResultTemp;
      $scope.test3 = response. resultType;
    }).error(function(error){
      console.log(error);
    });


    //get data
    $scope.data = statisticsService.getData();

});

//LineCtrl
var app = angular.module("statisticsApp", ["chart.js"]);
app.controller("LineCtrl", function ($scope, statisticsService) {
"use strict";
$scope.labels = ["0200", "0400", "0600", "0800", "1000", "1200", "1400", 
"1600", "1800", "2000", "2200", "0000"];
$scope.series = ['Temperature (°C)'];
$scope.data = [
[26.5, 26.8, 26.3, 25.8, 29.4, 30.2, 31.5, 31.0, 28.4, 27.6, 26.3, 25.7]
];

//set data
statisticsService.setData($scope.data);

$scope.onClick = function (points, evt) {
console.log(points, evt);
 };
});
...