2013-06-05 6 views
16

मेरे पास एक नियंत्रक है जो 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 

उत्तर

5

संसाधन निर्माता में पैरामीटर को परिभाषित करना ही जरूरी हैं जब कॉल जब कोई भी एक डिफ़ॉल्ट की जरूरत है कोशिश कर रहे हैं करने के लिए पर्याप्त करीब है भेज दिया गया है। विधि में पास किए गए किसी भी पैरा को क्वेरी पैरा के रूप में जोड़ा जाता है, भले ही उसके पास डिफ़ॉल्ट मान परिभाषित किया गया हो। '@' का अर्थ है कि पैराम मान को जेएसओएन प्रतिक्रिया में वापस लौटाया जाता है, इसलिए आपका @uuid समझ में आता है, लेकिन आपका @ माह नहीं।

व्यक्तिगत रूप से, मैं सिर्फ इतना की तरह संसाधन बना होगा:।

$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', isArray: true} 
    }); 

फिर उन्हें में पारित करके आवश्यकतानुसार क्वेरी चर जोड़ें (विशेष विधि के नाम बनाना ठीक है लेकिन आवश्यक तो नीचे दोनों तरीकों से दिखाई नहीं दे रहा।)

var t = new Transaction(); 
$scope.recentTransactions = t.$get({recent:true}) //results in /users/bd675d42-aa9b-11e2-9d27-b88d1205c810/transactions/?recent=true 
$scope.recentTransactions = t.$getRecent(); //same thing as above 
$scope.transactions = t.$get({month: $scope.month, year: $scope.year}); //results in /users/bd675d42-aa9b-11e2-9d27-b88d1205c810/transactions/?month=5&year=2013 
$scope.transactions = t.$getForMonthAndYear({month: $scope.month, year: $scope.year}); //same as above... since no defaults in constructor, always pass in the params needed 
+0

मुझे लगता है कि उसकी समस्या $ संसाधन के साथ नहीं है, बल्कि नियंत्रक और सेवा के बीच बातचीत – mfeingold

3

एक का कहना है यह ऐसा करने का तरीका है अपने नियंत्रक में सेवा इंजेक्षन करने के लिए है:

angular.controller('controllerName', 
['$scope', 'Transaction', 
    function($scope, transactionService) { 
     ... 
    } 
]); 

तो फिर तुम transactionService पैरामीटर के माध्यम से आपकी सेवा का एक उदाहरण का उपयोग कर सकते हैं।

संपादित

बाहर इस fiddle जांच मुझे बताओ अगर यह तुम क्या करने

+0

क्या आप कृपया मुझे बता सकते हैं कि कैसे? मैंने अभी अपना प्रश्न – daydreamer

+0

@daydreamer को आजमाया और अपडेट किया: आपकी लेनदेन सेवा 'लेनदेन सेवा' नामक मॉड्यूल में रहती है। सुनिश्चित करें कि आपके नियंत्रक को परिभाषित मॉड्यूल इसे इसकी निर्भरता के रूप में निर्दिष्ट करता है। angular.module ('myCtrlModule', ['लेनदेन सेवा']) नियंत्रक (... – Rao

5

मैं एक ही समस्या थी, मैं अपने सेवा से parametrized कार्यों लौटने समाप्त हो गया है, तो सेवा है कि

factory('MyService', function($resource) { 
    return { 
     id: function(param) { return $resource('/api/'+param+'/:id', {id: '@id'}); }, 
     list: function(param) { return $resource('/api/'+param); }, 
     meta: function(param) { return $resource('/api/meta/'+param); } 
    } 
}) 

तरह लग रहा है और साथ नियंत्रक से सेवा को फोन:

$scope.meta = MyService.meta('url_param').query(); 

यह सुनिश्चित नहीं है कि यह सबसे स्वच्छ angular.js समाधान है, लेकिन यह काम करता है!

संबंधित मुद्दे