gpt4 book ai didi

javascript - 将数据从 AngularJs 服务传递到 Controller 以及 Controller 到 Controller

转载 作者:行者123 更新时间:2023-11-28 17:48:37 25 4
gpt4 key购买 nike

目前我能够将值从 AngularJS 服务传递到 Angular Controller ,但是现在我正在一个 Controller 中进行一些计算,现在我想将其传递到另一个 Controller ,你能帮我吗

  • 服务名称:detailService

  • Controller 1:FromService

  • Controller 2:BarCtrl

  • 在下面的代码中,我从detailService获取值FromService Controller
  • 在 FromService Controller 中,我正在评估 Array 的值$scope.valuesSendToGraph

我想将这个 $scope.valuesSendToGraph 发送到 BarCtrl Controller ,你能帮我吗,如何解决这个问题

AngularJS 代码

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 am doing some calculation in one controller and now I want to pass it to another controller

我强烈建议您将所有计算移至服务中。 Controller 负责将数据与 View 绑定(bind)。

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

由于Service是单例的,您可以将valuesSendToGraph存储在服务中并从其他 Controller 加载它

<小时/>

如果您仍想在 Controller 之间发送 valuesSendToGraph 内容,并且它们具有相同的级别,则可以在根级别上使用 $broadcast,例如:

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

BarCtrl 中调用:

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

请确保在 $rootScope.$broadcast 之前调用 $scope.$on('values',)

关于javascript - 将数据从 AngularJs 服务传递到 Controller 以及 Controller 到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46142178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com