に依存するコントローラーがあり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
は と の 2 つのパラメータに依存して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