Ho un controller che dipende da TransactionService
. Uno dei metodi èAngularJS: come passare valori dal metodo Controller a servizio?
$scope.thisMonthTransactions = function() {
$scope.resetTransactions();
var today = new Date();
$scope.month = (today.getMonth() + 1).toString();
$scope.year = today.getFullYear().toString();
$scope.transactions = Transaction.getForMonthAndYear();
};
Il TransactionService
sembra
angular.module('transactionServices', ['ngResource']).factory('Transaction', function ($resource, $rootScope) {
return $resource('/users/:userId/transactions/:transactionId',
// todo: default user for now, change it
{userId: 'bd675d42-aa9b-11e2-9d27-b88d1205c810', transactionId: '@uuid'},
{
getRecent: {method: 'GET', params: {recent: true}, isArray: true},
getForMonthAndYear: {method: 'GET', params: {month: 5, year: 2013}, isArray: true}
});
});
Come si può vedere il metodo getForMonthAndYear
dipende da due parametri month
e year
, che sono fissi in questo momento come params: {month: 5, year: 2013}
. Come posso trasferire questi dati dal mio controller?
Ho provato a iniettare rootScope
in TransactionService
, ma ciò non ha aiutato (nel senso che non so come usarlo probabilmente).
Anche Angular ngResource documentation non consiglia alcun metodo per eseguire questa operazione.
Qualcuno può guidare qui?
UPDATE
mio controller sembra
function TransactionsManagerController($scope, Transaction) {
$scope.thisMonthTransactions = function() {
$scope.resetTransactions();
var today = new Date();
$scope.month = (today.getMonth() + 1).toString();
$scope.year = today.getFullYear().toString();
var t = new Transaction();
$scope.transactions = t.getForMonthAndYear({month: $scope.month});
};
}
e cambiare metodo di servizio ai
getForMonthAndYear: {method: 'GET', params: {month: @month, year: 2013}, isArray: true}
guardo il console.log
e dice
Uncaught SyntaxError: Unexpected token ILLEGAL transaction.js:11
Uncaught Error: No module: transactionServices
Penso che il suo problema non sia con la risorsa $, ma piuttosto l'interazione tra il controller e un servizio – mfeingold