Passing the data from AngularJs Service to Controller and Controller to Controller

52 views Asked by At

Currently I am able to pass the value from the AngularJS Service to Angular Controller, But Now I am doing some calculation in one controller and now I want to pass it to another controller , Can you please help me

  • Service Name : detailService

  • Controller 1 : FromService

  • Controller 2 : BarCtrl

  • In the below Code I am getting the Values from detailService to FromService Controller
  • And In the FromService Controller I am evaluating the value for Array $scope.valuesSendToGraph

I wanted to send this $scope.valuesSendToGraph to BarCtrl Controller , Can you please help me , How to approach this Problem

AngularJS Code

var app = angular.module('myApp', ["chart.js"]);
app.service('detailService', function($http, $filter, $q) {
  return {
    getInfo: function() {
      return $http({
        method: 'POST',
        url: 'myjson.json'
      }).then(function(response) {
        msoResponseArray = response.data;
        var passByCompanyArray = [];
        var failByCompanyArray = [];
        var wipByCompanyArray = [];
        var releasenameArray = [];
        var totalCountArray = [];
        var fullArray = [];

        msoResponseArray.forEach(function(item) {
          releasenameArray.push(item.executions[0].cycleName);
        });

        msoResponseArray.forEach(function(item) {
          /* Filtering the Items Which are Passed*/
          passByCompany = $filter('filter')(item.executions, function(
            inputs) {
            if ((inputs.executedBy == 'a' || inputs.executedBy == 'b' || inputs.executedBy == 'c' || inputs.executedBy == 'd') && inputs.executionStatus == '1') return inputs;

          });
          passByCompanyArray.push(passByCompany.length);
          //passByCompanyArray.unshift(item.executions[0].cycleName);
          failByCompany = $filter('filter')(item.executions, function(
            inputs) {
            if ((inputs.executedBy == 'a' || inputs.executedBy == 'b' || inputs.executedBy == 'c' || inputs.executedBy == 'd') && inputs.executionStatus == '2') return inputs;
          });
          failByCompanyArray.push(failByCompany.length);
          //failByCompanyArray.unshift(item.executions[0].cycleName);

          wipByCompany = $filter('filter')(item.executions, function(
            inputs) {
            if ((inputs.executedBy == 'a' || inputs.executedBy == 'b' || inputs.executedBy == 'c' || inputs.executedBy == 'd') && inputs.executionStatus == '3') return inputs;

          });
          wipByCompanyArray.push(wipByCompany.length);
          //wipByCompanyArray.unshift(item.executions[0].cycleName);

        });
        return $q.when([releasenameArray, passByCompanyArray, failByCompanyArray, wipByCompanyArray]);
      })
    }


  }
});
app.controller('FromService', function($scope, detailService) {
  detailService.getInfo().then(function(data) {
    $scope.newvalue = data;
    $scope.releasename = data[0];
    $scope.passCount = data[1];
    $scope.failCount = data[2];
    $scope.wipCount = data[3];

    function sumValue(arr) {
      var total = 0;
      arr.forEach(function(element) {
        total += element;
      })
      return total;
    }
    var sumPassCount = sumValue($scope.passCount);
    var sumFailCount = sumValue($scope.failCount);
    var sumWipCount = sumValue($scope.wipCount);
    $scope.valuesSendToGraph = [sumPassCount, sumFailCount, sumWipCount];
    console.log($scope.valuesSendToGraph);


    $scope.fullArray = $scope.releasename.map(function(item, i) {
      return {
        releasename: item,
        passCount: $scope.passCount[i],
        failCount: $scope.failCount[i],
        wipCount: $scope.wipCount[i]
      }
    });
    console.log($scope.fullArray);
  });
});
app.config(function(ChartJsProvider) {
  // Configure all charts
  ChartJsProvider.setOptions({
    colors: ['#803690', '#00ADF9', '#DCDCDC', '#46BFBD', '#FDB45C', '#949FB1', '#4D5360'],
    defaultFont: 'Ve'


  });
  // Configure all doughnut charts
  ChartJsProvider.setOptions('doughnut', {
    cutoutPercentage: 60
  });
  ChartJsProvider.setOptions('bubble', {
    tooltips: {
      enabled: false
    }
  });
});
app.controller('BarCtrl', function($scope) {
  $scope.labels = ['Passed', 'Failed', 'WIP'];
  $scope.series = ['Regression'];

  $scope.data = [
    $scope.valuesSendToGraph
  ];
});
1

There are 1 answers

1
Maxim Shoustin On BEST ANSWER

I am doing some calculation in one controller and now I want to pass it to another controller

I strongly recommend you do move all calculations to service. Controller is responsible to bind data with view.

I wanted to send this $scope.valuesSendToGraph to BarCtrl Controller

Since Service is singleton, you can store valuesSendToGraph in service and load it from other controller


If you still want to send valuesSendToGraph content from controller to controller and they have the same level, you can use $broadcast on root level, for example:

$rootScope.$broadcast('values', $scope.valuesSendToGraph);

In BarCtrl call:

$scope.$on('values', function(event, data) {
  $scope.valuesSendToGraph = data;
});

Be sure that you call $scope.$on('values', before $rootScope.$broadcast