Deepak Verma
Deepak Verma

Reputation: 73

How to access data of [object object] AngularJS

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.

Upvotes: 0

Views: 998

Answers (2)

Weedoze
Weedoze

Reputation: 13943

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

Upvotes: 1

Senal
Senal

Reputation: 1610

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)

Upvotes: 1

Related Questions