我正在嘗試用Angularjs創建一個應用程序,這是一個相當大的應用程序,需要將其分解爲多個控制器。我需要跨控制器進行計算。Angularjs - 交叉控制器計算
angular.module('Cross.Controller.demo',[]);
angular.module('Cross.Controller.demo').controller('KidsCtrl', function ($scope) {
$scope.Kids = [
{"Name":"John", "Expense":"1000"},
{"Name":"Anna", "Expense":"900"}];
});
angular.module('Cross.Controller.demo').controller('HouseCtrl', function ($scope) {
$scope.House = {"Category":"Utilities", "Expense":"2000"};
});
angular.module('Cross.Controller.demo').controller('ResultCtrl', function ($scope) {
$scope.Result = {"Category":"Total", "Expense":"2000"};
});
<!doctype html>
<html ng-app="Cross.Controller.demo">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="script.js"></script>
<body>
<div ng-controller="KidsCtrl">
<div ng-repeat="kid in Kids">
{{kid.Name}}
<input type="text" ng-model="kid.Expense">
</div>
</div>
<div ng-controller="HouseCtrl">
{{House.Category}}
<input type="text" ng-model="House.Expense">
</div>
<div ng-controller="ResultCtrl">
{{Result.Category}}
<input type="text" ng-model="Result.Expense">
</div>
</body>
</html>
請找我Plunk這裏。
我想從孩子和開銷的房子既增加開支的結果控制器的「Result.Expense」
工作如何創建服務/工廠對象,並具有三個與服務交互執行你的計算? – Chris