How do I obtain $stateParams in Angular sevice

Here is my state definition:

    templateUrl: "templates/sub-topics.html",
    controller: "SubTopicsController"

Here is my service

myApp.service('subTopicsService',function($http, $stateParams){

    this.getSubTopics = function(){
        $http.get('topics/' + $stateParams.topicId + '.json').success(function(data, status, header, config) {
             return data;

A part of my controller, that has 'subTopicService' injected

$scope.topicList = subTopicsService.getSubTopics();

The problem is, the $stateParams is undefined in the service. Specifically I get this error message:

Cannot access property :topicId of undefined.
How do I use $stateParams in my service?

You have to pass it from controller, try it:


this.getSubTopics = function(paramsFromCtrl) {
    $http.get('topics/' + paramsFromCtrl.topicId + '.json').success(function(data, status, header, config) {
         return data;

and in controller:

$scope.topicList = subTopicsService.getSubTopics($stateParams);

Sweet. That is really simple.