Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS: example of two-way data binding with Resource
    text
    copied!<p>I have a view Transaction which has two sections</p> <p>a.) view-transaction<br> b.) add-transaction</p> <p>both are tied to the following controller</p> <pre><code>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); } } </code></pre> <p>, where Transaction is a service and looks as follows</p> <pre><code>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' }); }); </code></pre> <p>When i click on tab "Transaction", the route #/transactions is activated, causing it to render both sub-views a.) and b.)</p> <p>The question that I have is,<br> - Is there a way to update the $scope.transactions whenever I add new transaction? Since it is a resource<br> or I will have to manually do $scope.transactions.push(transaction); </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload