我剛剛開始使用Angular.js,我不確定如何將兩個「模型」「鏈接」在一起。我在我的index.php文件在Angular.js中連接模型
<div ng-controller="AccountCtrl">
<h2>Accounts</h2>
<ul>
<li ng-repeat="account in accounts">
<span>{{account.id}} {{account.ownedBy}}</span>
</li>
</ul>
</div>
<div ng-controller="TransactionCtrl">
<h2>Transactions</h2>
<ul>
<li ng-repeat="transaction in transactions">
<span>{{transaction.id}} {{transaction.timestamp}} {{transaction.amount}} {{transaction.description}} {{transaction.account}}</span>
</li>
</ul>
</div>
下面的代碼和下面的JS
function AccountCtrl($scope, $http) {
// initialize Data
$http({
method:'GET',
url:'http://api.mydomain.ca/accounts'
}).success(function(data, status, headers, config) {
$scope.accounts = data;
}).error(function(data, status, headers, config) {
alert('Error getting accounts. HTTP Response status code: '+status);
});
}
function TransactionCtrl($scope, $http) {
// initialize Data
$http({
method:'GET',
url:'http://api.mydomain.ca/transactions'
}).success(function(data, status, headers, config) {
$scope.transactions = data;
}).error(function(data, status, headers, config) {
alert('Error getting transactions. HTTP Response status code: '+status);
});
}
所以在我的例子中,每個帳戶將有許多事務,我想一個函數添加到我的帳戶控制器根據交易計算帳戶的餘額,但我不知道如何執行此操作,因爲它們處於不同的$範圍內。
有沒有辦法在Angular中做到這一點,或者當我得到帳戶時,是否必須從服務器返回JSON響應中的「鏈接」事務信息?