【发布时间】:2013-05-31 15:23:28
【问题描述】:
我有一个依赖于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}。如何从我的控制器传递这些数据?
我尝试在TransactionService 中注入rootScope,但这并没有帮助(意味着我可能不知道如何使用它)。
另外Angular ngResource documentation 不推荐任何方式来执行此操作。
有人可以在这里指导吗?
更新
我的控制器看起来像
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
【问题讨论】:
标签: javascript angularjs angularjs-scope angularjs-service