目前我能够将AngularJS服务中的值传递给Angular Controller,但是现在我在一个控制器中进行一些计算,现在我想将它传递给另一个控制器,你能帮我吗?
服务名称:detailService
控制器1:来自服务
控制器2:BarCtrl
我想把这个$ scope.valuesSendToGraph发送给BarCtrl控制器,你能帮我解决一下,如何解决这个问题
AngularJS代码
subprocess.call("C:/wamp64/bin/php/php5.6.31 C:/wamp64/www/index.php")
SET PATH=%PATH%;C:\wamp64\bin\php\php5.6.31
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);
});
});
答案 0 :(得分:3)
我正在一个控制器中进行一些计算,现在我想将它传递给另一个控制器
我强烈建议您将所有计算移动到服务中。 Controller负责将数据与视图绑定。
我想将这个$ scope.valuesSendToGraph发送给BarCtrl Controller
由于Service是singleton,您可以将<div >
<input type="checkbox" name="Example" checked onload="Check()" onclick="Check()" class="Check"/> Some text <br>
</div>
<div class="input" style="display:none;">
<input type="text" name="abc" class="input" > Som e text <br>
</div>
<script type="text/javascript">
Check(); //make sure we set the correct initial state
function Check() {
var checkbox=document.getElementsByClassName('Check')[0];
var example=document.getElementsByClassName('input')[0];
if(checkbox.checked){
example.style.display='block';
}else{
example.style.display='none';
}
}
</script>
存储在服务中并从其他控制器加载
如果您仍想将valuesSendToGraph
内容从控制器发送到控制器并且它们具有相同级别,则可以在根级别使用valuesSendToGraph
,例如:
$broadcast
在$rootScope.$broadcast('values', $scope.valuesSendToGraph);
来电:
BarCtrl
请务必在$scope.$on('values', function(event, data) {
$scope.valuesSendToGraph = data;
});
$scope.$on('values',