Reputation: 1276
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
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
];
});
Upvotes: 0
Views: 53
Reputation: 77920
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
Upvotes: 3