मेरे पास एक नियंत्रक है जो TransactionService
पर निर्भर है। विधि में से एकAngularJS: नियंत्रक से सेवा विधि से मूल्य कैसे पास करें?
$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}
});
});
लगता है कि आप दो पैरामीटर month
और year
, जो अभी params: {month: 5, year: 2013}
के रूप में hardcoded कर रहे हैं पर निर्भर करता है विधि getForMonthAndYear
देख सकते हैं है। मैं इस डेटा को अपने नियंत्रक से कैसे पास कर सकता हूं?
मैंने TransactionService
में इंजेक्शन करने का प्रयास किया, लेकिन इससे मदद नहीं मिली (जिसका अर्थ है कि मुझे यह नहीं पता कि इसका उपयोग कैसे किया जाए)।
भी 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
मुझे लगता है कि उसकी समस्या $ संसाधन के साथ नहीं है, बल्कि नियंत्रक और सेवा के बीच बातचीत – mfeingold