我有一个视图事务,它有两个部分
a.) 查看交易
b.) 添加交易
两者都绑定到以下控制器
function TransactionController($scope, Category, Transaction) {
$scope.categories = Category.query(function() {
console.log('all categories - ', $scope.categories.length);
});
$scope.transactions = Transaction.query();
$scope.save = function() {
var transaction = new Transaction();
transaction.name = $scope.transaction['name'];
transaction.debit = $scope.transaction['debit'];
transaction.date = $scope.transaction['date'];
transaction.amount = $scope.transaction['amount'];
transaction.category = $scope.transaction['category'].uuid;
//noinspection JSUnresolvedFunction
transaction.$save();
$scope.transactions.push(transaction);
console.log('transaction saved successfully', transaction);
}
}
,其中 Transaction 是一项服务,如下所示
angular.module('transactionServices', ['ngResource']).factory('Transaction', function($resource) {
return $resource('/users/:userId/transactions/:transactionId', {
// todo: default user for now, change it
userId: 'bd675d42-aa9b-11e2-9d27-b88d1205c810',
transactionId: '@uuid'
});
});
当我单击“事务”选项卡时,路由 #/transactions 被激活,导致它呈现子视图 a.) 和 b.)
我的问题是,
- 有没有办法在我添加新交易时更新 $scope.transactions?因为它是一种资源
,否则我将不得不手动执行 $scope.transactions.push(transaction);