0

I am new to AngularJS. I am sending a JsonObject to another state.

Ex:

viewScope.edit=function(d) {

   var viewData = {
      'name' : d.name,
   };

   $state.go('edit', {'viewD': viewData}, {reload: true});
};

My State is-

viewApp.config(function($stateProvider){
    $stateProvider
        .state('edit', {
            name: '#/edit',
            url: '/register/{viewD}',
            templateUrl: function(){    
                 return path+'/register.jsp';
             },
             controller:'registerCtrl',
             resolve : {
                 loadPlugin: function($ocLazyLoad) {
                     return $ocLazyLoad.load([{
                         name : 'registerApp',
                         files: [path+'/resources/js/register.js'],
                     }])
                }
            }
        })
});

In register Controller getting data-

regApp.controller('registerCtrl',function($stateParams){                 
        if($stateParams != undefined){
            console.log($stateParams.viewD);
        }
});

On console output is- [object object]

How can i access the name key from this [object object].

console.log($StateParams.viewD.name); // Not Working

JSON.parse, JSON.stringify not working.

8
  • What is the content of $stateParams ? What is the type of $stateParams.viewD ? Commented Apr 20, 2018 at 5:42
  • @Weedoze The content of $stateParams is viewD : "[object Object]" Commented Apr 20, 2018 at 5:50
  • Where do you see this [object Object] ? In the console ? Can you expand it ? What is the result of console.log(typeof $stateParamas.viewD) Commented Apr 20, 2018 at 6:22
  • @Weedoze yes in the console. No i can not expand it. Its type is String Commented Apr 20, 2018 at 6:30
  • 2
    Try to replace your url in the state config by url: '/register/{viewD:json}', Commented Apr 20, 2018 at 6:40

2 Answers 2

1

You have to change your state config URL from

url: '/register/{viewD}',

to

url: '/register/{viewD:json}',

The JSON parameter type has been added in version 0.2.13

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

Comments

1

Change your config method as following,

viewApp.config(function($stateProvider){
$stateProvider
    .state('edit', {
        name: '#/edit',
        url: '/register',
        params: {
           viewD: null
        }
        templateUrl: function(){    
             return path+'/register.jsp';
         },
         controller:'registerCtrl',
         resolve : {
             loadPlugin: function($ocLazyLoad) {
                 return $ocLazyLoad.load([{
                     name : 'registerApp',
                     files: [path+'/resources/js/register.js'],
                 }])
            }
        }
    })
});

And then you can access your object from $state like this in the controller, $state.params.viewD or from $stateParams like this $stateParams.viewD

Now try console.log($state.params.viewD.name)

6 Comments

console.log($stateParams.viewD); output= {name : "abc"} but console.log($stateParams.viewD.name); gives error during app load undefined name.
Try this and see the type of your viewD console.log(typeof $stateParams.viewD), if it's a string JSON.parse($stateParams.viewD).name would return the name
Give error --- SyntaxError: Unexpected token u in JSON at position 0 on JSON.parse($stateParams.viewD).name
Seems like your $stateParams.viewD object is undefined. if($stateParams.viewD) JSON.parse($stateParams.viewD).name try this. If it doesn't go inside the if check your source of the viewD
Its not undefined it came under if($StateParams.viewD){ and still error. What i do?
|

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.