[英]Pass object id to session storage
I have a simple function to post the selected object ID to session storage it works well as numbers but when i change it to a word it dose not work, any idea why and how to fix it? 我有一个简单的功能,将选定的对象ID发布到会话存储中,它可以很好地用作数字,但是当我将其更改为单词时,它却无法正常工作,不知道为什么以及如何解决它吗?
I thought it would be fine as they are both a string, as you can see my function at the bottom parse the json, I dont see why it only works for numbers ? 我认为这都很好,因为它们都是字符串,因为您可以在底部看到我的函数解析json,但我不明白为什么它仅适用于数字?
Code 码
$scope.productsandformats = [{
"name": "name 1",
"format": [{
"Fname": "test (ROADSIDE TEMP)",
"id": "Roadside"
}, {
"Fname": "test Sheet",
"id": "2"
}, {
"Fname": "Wrap test (Digital)",
"id": "3"
}]
}, {
"name": "name 2,
"format": [{
"Fname": "2 test",
"id": "4"
}, {
"Fname": "test Live (Digital)",
"id": "5"
}]
};
$scope.productTypeChange = function() {
$scope.formats = $scope.productsandformats.find(ps => ps.name === $scope.formData.ProductType.name)
//NG-Change
$scope.myFunc = function() {
var jsonItem = JSON.parse($scope.formData.formatType.id);
sessionStorage.setItem('format', jsonItem);
}
}
});
I Get a error in inspector 我在检查器中出现错误
SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse () SyntaxError:JSON中的意外令牌R在JSON.parse()的位置0
try the below json in $scope
在$scope
尝试以下json
$scope.productsandformats =[{
"name": "name 1",
"format": [{
"Fname": "test (ROADSIDE TEMP)",
"id": "Roadside"
}, {
"Fname": "test Sheet",
"id": "2"
}, {
"Fname": "Wrap test (Digital)",
"id": "3"
}]
}, {
"name": "name 2",
"format": [{
"Fname": "2 test",
"id": "4"
}, {
"Fname": "test Live (Digital)",
"id": "5"
}]
}]
The ID you are parsing isn't JSON, that's why you're getting the error. 您正在解析的ID不是JSON,这就是为什么您会收到错误消息。
In point of fact the whole scope object is just a JavaScript object, not JSON (which is a text format). 实际上,整个范围对象只是一个JavaScript对象,而不是JSON(一种文本格式)。
The Id that you get is the string "Roadside", but if it's a JSON string it'd look something like "{ \\"key\\": \\"value\\" }". 您获得的ID是字符串“ Roadside”,但是如果它是JSON字符串,则其外观类似于“ {\\\\ key \\“:\\” value \\“}”。 The fact that it's missing the opening brace tells the parser it's wrong. 缺少左括号的事实告诉解析器这是错误的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.