Mam kontroler, który jest zależny od TransactionService
. Jedną z metod jestAngularJS: Jak przekazywać wartości z kontrolera do metody serwisowej?
$scope.thisMonthTransactions = function() {
$scope.resetTransactions();
var today = new Date();
$scope.month = (today.getMonth() + 1).toString();
$scope.year = today.getFullYear().toString();
$scope.transactions = Transaction.getForMonthAndYear();
};
TransactionService
wygląda
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}
});
});
Jak widać metody getForMonthAndYear
zależy od dwóch parametrów month
i year
, które są sztywno teraz jako params: {month: 5, year: 2013}
. Jak mogę przekazać te dane z kontrolera?
Próbowałem wstrzykiwać rootScope
w TransactionService
, ale to nie pomogło (co oznacza, że nie wiem jak tego użyć).
Również Angular ngResource documentation nie zaleca żadnego sposobu wykonania tej czynności.
Czy ktoś może tu podać przewodnik?
UPDATE
moim kontroler wygląda
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});
};
}
i zmienić sposób obsługi do
getForMonthAndYear: {method: 'GET', params: {month: @month, year: 2013}, isArray: true}
patrzę na console.log
i mówi
Uncaught SyntaxError: Unexpected token ILLEGAL transaction.js:11
Uncaught Error: No module: transactionServices
Myślę, że jego problem nie dotyczy zasobu $, ale raczej interakcji między kontrolerem a usługą – mfeingold