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

Pavan S

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);
    console.log(userObj);
    $scope.userObj = userObj;

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

    $scope.FBout = function(){
        FB.logout(function(response) {
            authFactory.clearCookie();
            $location.path("/");    
            $scope.$apply();
        });
    };
}]);

In the console, I get the response as follows

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

The factory code for the same is as shown below:

app.factory("authFactory",["$cookies","$location",function($cookies,$location){
    var authFactory = {};

    authFactory.setAccessToken = function(accessToken){
        $cookies.put("accessToken",accessToken);
    }

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

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

        if(userObj){
            return userObj;
        }
        else {
            console.log("error");
        }
    }

    authFactory.clearCookie = function(){
        $cookies.remove("accessToken");
        $cookies.remove("userObj");
    }

    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}} {{userObj.name}} </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 {{userObj}}, the whole object will be printed. then if I try {{userObj.name}} then I should get the name value of that user object, right?

Luke Taylor

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not able to print my json data in my console?

From Dev

Not able to print my json data in my console?

From Dev

How can I print json array in Angular 4?

From Dev

I can print the value in console but not in html view

From Dev

Is it normal for js console and rails console to print the same time differently?

From Dev

How Can i Print 2 Works by threads side by side in same column in c# Console?

From Dev

Is for Node.js similar package where I can print console.log based on deploy environments?

From Dev

How to print Json result to view in angular js

From Dev

How can I see print() statements in behave (BDD)

From Dev

How can I see Debug.Print() statements?

From Dev

Where can I print out logs to see immediately in Android Studio?

From Dev

Where can I see what print_r display on PHP?

From Dev

How can I print these values (see description) in a table?

From Dev

How can I manage to make stderr messages NOT print in the console?

From Dev

How can I print reported bugs to console in gradle findbugs plugin?

From Dev

How can I print to the console when using knitr?

From Dev

How can I print to the console this table of numbers in Java?

From Dev

How can I manage to make stderr messages NOT print in the console?

From Dev

Stuck and can´t print what i want in console Java

From Dev

How can I print to console after button press?(Python 3.5)

From Dev

How can i print a specific amount of characters per line in the console?

From Dev

How can i calculate the response time in the below code and print it on the console

From Dev

Can I print something with console.log() in Promise? (DEBUGGING)

From Dev

Can't print to console in robocode

From Dev

Can't print String in console

From Dev

the JSON works but print an error in console

From Dev

Not able to print value of object in angular2

From Dev

Why wont console print values of i under 2 in FOR loop if it can print values over 2?

From Dev

Not able to print JSON object String in android TextView

Related Related

  1. 1

    Not able to print my json data in my console?

  2. 2

    Not able to print my json data in my console?

  3. 3

    How can I print json array in Angular 4?

  4. 4

    I can print the value in console but not in html view

  5. 5

    Is it normal for js console and rails console to print the same time differently?

  6. 6

    How Can i Print 2 Works by threads side by side in same column in c# Console?

  7. 7

    Is for Node.js similar package where I can print console.log based on deploy environments?

  8. 8

    How to print Json result to view in angular js

  9. 9

    How can I see print() statements in behave (BDD)

  10. 10

    How can I see Debug.Print() statements?

  11. 11

    Where can I print out logs to see immediately in Android Studio?

  12. 12

    Where can I see what print_r display on PHP?

  13. 13

    How can I print these values (see description) in a table?

  14. 14

    How can I manage to make stderr messages NOT print in the console?

  15. 15

    How can I print reported bugs to console in gradle findbugs plugin?

  16. 16

    How can I print to the console when using knitr?

  17. 17

    How can I print to the console this table of numbers in Java?

  18. 18

    How can I manage to make stderr messages NOT print in the console?

  19. 19

    Stuck and can´t print what i want in console Java

  20. 20

    How can I print to console after button press?(Python 3.5)

  21. 21

    How can i print a specific amount of characters per line in the console?

  22. 22

    How can i calculate the response time in the below code and print it on the console

  23. 23

    Can I print something with console.log() in Promise? (DEBUGGING)

  24. 24

    Can't print to console in robocode

  25. 25

    Can't print String in console

  26. 26

    the JSON works but print an error in console

  27. 27

    Not able to print value of object in angular2

  28. 28

    Why wont console print values of i under 2 in FOR loop if it can print values over 2?

  29. 29

    Not able to print JSON object String in android TextView

HotTag

Archive