Pavan S Pavan S - 1 year ago 120
AngularJS Question

Print the JSON in angular js, I can see in console, but not able to print the same

I am missing something very simple.

In my controller I am getting the JSON object.

app.controller("dashboardCtrl", ["$scope","authFactory","$location", function($scope,authFactory,$location){
var userObj = authFactory.getUserObj(userObj);
$scope.userObj = userObj;

var accessToken = authFactory.getAccessToken();
$scope.accessToken = accessToken;

$scope.FBout = function(){
FB.logout(function(response) {

In the console, I get the response as follows

{"name":"Pavan Sudheendra","id":"539193599614696"}

The factory code for the same is as shown below:

var authFactory = {};

authFactory.setAccessToken = function(accessToken){

authFactory.getAccessToken = function(){
return $cookies.get("accessToken");

authFactory.getUserObj = function(){
var userObj = $cookies.get('userObj');

return userObj;
else {

authFactory.clearCookie = function(){

authFactory.isAuthenticated = function(){
var isLoggedIn=$cookies.get("accessToken")?true:false;
return isLoggedIn;

return authFactory;

but I am trying to use that object, to print it in the front end. Like

<h1> this is dashboard {{accessToken}} {{}} </h1>

I am sure I have included every controller and linked the factory to it, and the thing is I am able to print the accesstoken. However I can't print the name and id of it.

If I try to print
, the whole object will be printed. then if I try
then I should get the name value of that user object, right?

Answer Source

As discussed in the comments, your cookie stores a string, not an object. You can get an object back from your JSON string using the angular.fromJson method.

$scope.userObj = angular.fromJson(userObj)

You could potentially simplify things further by using the $cookies service's getObject and putObject methods, letting Angular serialize and deserialize the object in the background.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download