将json对象添加到$ cookies - Angular JS

use*_*111 6 javascript cookies json angularjs

在控制台中我的代码是打印:

[对象,对象]

我无法正常向下钻取.

我试过JSON.ParseJSON.stringify没有成功.

我的服务设置凭据:

setCredentials.js

'use strict';

// service that handles setting and getting cookies  

app.service('setCredentials', function($cookies) {

    // function that gets json object and stores it inside a cookie
    this.storeInCookie = function(responseObject) {

        console.log(responseObject);

        //set the cookie
        var cookieObj = {
            currentUser: {
                userType: responseObject.auth.userType,
                username: responseObject.auth.email,
                token: responseObject.auth.token
            }
        };


        console.log(cookieObj);
        //set up header, in case we need auth token inside it for later 
        //$http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; 
        //store inside of cookie
        $cookies.put('globals', cookieObj);

        return true;

    };

    //function to remove cookie
    this.removeCookie = function() {



    };

    //function to get cookie
    this.getCookie = function(cookieName) {

        //get cookie
        return $cookies.get(cookieName);

    };

});
Run Code Online (Sandbox Code Playgroud)

然后我在控制器中调用cookie对象:

navigationController.js

'use strict';

//app global variable
//this is the controller that handles post requests
//declare services as dependecies $http, $location, custom service apiServiceWeb
app.controller('navigationController', function($scope, $rootScope, $http, $location, $cookies, setCredentials) {


  //navigation menu

  //get what is in the cookie
  var cookieValue = setCredentials.getCookie('globals');
  console.log($cookies.get('globals'));

});
Run Code Online (Sandbox Code Playgroud)

Est*_*ask 17

使用

$cookies.putObject('globals', cookieObj)
Run Code Online (Sandbox Code Playgroud)

$cookies.getObject('globals')
Run Code Online (Sandbox Code Playgroud)

代替.