0

I am trying to pass an object to a controller via $state.go().

app.js :

.state('class', {
    url: '/class/:programId',

   views: {                        
        'content@': {
            templateUrl: 'class.html',
            controller: 'ClassController'
        },
        data: {
            requireLogin: true
        }
    }
}).state('classLevel', {
    url: '/classLevel',
   views: {

        'content@': {
            templateUrl: 'partials/classLevel.html',                            
            controller: 'ClassLevelController',
            params: { obj: null }

        },
        data: {
            requireLogin: true
        }
    }
})

controller :

controllers.controller('ClassController', ['$scope', '$state', '$rootScope', '$stateParams', 'myService',
function($scope, $state, $rootScope, $stateParams) {

   var myClass = myService.getList(url here);
      myClass.then(function (data) {
            // getting data here
      });


   $scope.loadClassLevel = function(summaryId, sessionId){
    var CurrentDate = moment().unix();
    var yourObj = {};
    yourObj.currentTime = CurrentDate;
    yourObj.summaryId = summaryId;
    yourObj.sessionId = sessionId;
    $state.go('patientFeelingLevel',{obj:yourObj});             
  }

});


controllers.controller('ClassLevelController', ['$scope', '$state', '$rootScope', '$stateParams',
        function($scope, $state, $rootScope, $stateParams) {
            console.log("$state :",$state);
});

I am trying to pass object from ClassController to ClassLevelController. But in console.log I am getting

params: Object
       obj: null

Help me how to pass that object to next controller (ClassLevelController here).

Nagarjuna Reddy
  • 4,083
  • 14
  • 41
  • 85
  • 1
    Possible duplicate of [How to pass custom data in $state.go() in angular-ui-router?](http://stackoverflow.com/questions/30165272/how-to-pass-custom-data-in-state-go-in-angular-ui-router) – Heretic Monkey Apr 29 '16 at 17:53
  • @Mike I tried that but it is not working. Don't know where I did wrong. – Nagarjuna Reddy Apr 29 '16 at 17:56
  • Can you create some jsfiddle/plunker with your example that does not work? That will help to find where you did wrong. – MaKCbIMKo Apr 30 '16 at 00:06

1 Answers1

1

You should use not $state to access the parameters that you passed, but $stateParams service.

Try this one:

console.log("$stateParams :",$stateParams);

That's because your state object will be always the same as at the start of your app.

Here is some example which shows that $stateParams is changing, but $state isn't.

MaKCbIMKo
  • 2,800
  • 1
  • 19
  • 27
  • Thanks for reply. I tried this: **http://stackoverflow.com/questions/30165272/how-to-pass-custom-data-in-state-go-in-angular-ui-router** – Nagarjuna Reddy Apr 29 '16 at 16:31
  • That should work. I played a bit with plunked and in both cases (with $state and $stateParams) it works. And both of them has my passed object. Can you share your ClassLevelController? – MaKCbIMKo Apr 29 '16 at 16:44