我在控制台中打印了cookie数据现在我想创建一个从cookie firstname,lastname,eamil和id获取数据的对象。当我以下面的格式接收数据时,我如何创建对象?
mainCtrl.js
const bars = [];
Object.keys(this.props.params).forEach(key => {
const value = this.props.params[key];
bars.push(
<ProgressBar
key={key}
property={key}
value={Number(value)} />
);
});
打印数据$ scope.cookie
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)
});
|
答案 0 :(得分:1)
您可以使用split
分隔值。您应该知道不鼓励这样做,因为如果格式更改,您的代码将被破坏。但是如果你真的需要解析那个字符串,你可以试试这个:
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