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
];
});
I strongly recommend you do move all calculations to service. Controller is responsible to bind data with view.
Since Service is singleton, you can store
valuesSendToGraph
in service and load it from other controllerIf 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:In
BarCtrl
call:Be sure that you call
$scope.$on('values',
before$rootScope.$broadcast