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).

3
  • 1
    Possible duplicate of How to pass custom data in $state.go() in angular-ui-router? Commented Apr 29, 2016 at 17:53
  • @Mike I tried that but it is not working. Don't know where I did wrong. Commented Apr 29, 2016 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. Commented Apr 30, 2016 at 0:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply. I tried this: stackoverflow.com/questions/30165272/…
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.