简体   繁体   中英

How to extract data from cookie using angular ngCookie?

I have cookie data printed in console now i want to create object that will get data from cookie firstname ,lastname,eamil and id. How i can create object when i recieve data in below format ?

mainCtrl.js

angular.module('angularModelerApp')
  .controller('AccessCtrl',['$scope', '$cookies','UserAccessFactory',
    function ($scope, $cookies,UserAccessFactory) {
      $scope.newUser = {};
      $scope.patternAttuid = new RegExp("^[a-z]{2}[0-9]{3}[a-z0-9]$");
      $scope.cookie = $cookies.get('attESHr');
      console.log('newUser',$scope.cookie)

    });

data printed $scope.cookie

newUser Mike|Pierro|mp529u@us.att.com|||sl3561||mp529u,RHCRSMK,SBGPQX9,4131585|NNNNNNNNNNNNNNYNNYNNNNNN|MIKE|EY1PE2600 |

You can use split to get the values separated. You should know that doing this is not encouraged because if the format changes, your code will be broken. But if you really need to parse that string, you could try this:

var parts = $scope.cookie.split("|"); 
$scope.newUser.firstName = parts[0];
$scope.newUser.lastName = parts[1];
$scope.newUser.email = parts[2];
// ... etc with all the other values, keeping in mind the index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM