我有一个依赖于TransactionService
. 其中一种方法是
$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
像
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}
});
});
如您所见,该方法getForMonthAndYear
取决于两个参数month
和year
,它们现在被硬编码为params: {month: 5, year: 2013}
. 如何从我的控制器传递这些数据?
我尝试注入rootScope
,TransactionService
但这没有帮助(这意味着我可能不知道如何使用它)。
Angular ngResource文档也不推荐任何方式来执行此操作。
有人可以在这里指导吗?
更新
我的控制器看起来像
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});
};
}
我将服务方法更改为
getForMonthAndYear: {method: 'GET', params: {month: @month, year: 2013}, isArray: true}
我看着console.log
它,它说
Uncaught SyntaxError: Unexpected token ILLEGAL transaction.js:11
Uncaught Error: No module: transactionServices