将对象 ID 传递给会话存储
Pass object id to session storage
我有一个简单的功能,可以将选定的对象 ID post 存储到会话存储中,它与数字一样工作良好,但是当我将其更改为单词时,它不起作用,知道为什么以及如何修复它吗?
我认为这很好,因为它们都是字符串,正如您在底部看到我的函数解析 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"
}]
};
$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);
}
}
});
我在检查器中遇到错误
SyntaxError: Unexpected token R in JSON at position 0
at JSON.parse ()
在 $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"
}]
}]
您正在解析的 ID 不是 JSON,这就是您收到错误的原因。
事实上,整个范围对象只是一个 JavaScript 对象,而不是 JSON(这是一种文本格式)。
您获得的 ID 是字符串 "Roadside",但如果它是 JSON 字符串,它看起来类似于 "{ \"key\": \"value\" }”。它缺少左大括号的事实告诉解析器它是错误的。
我有一个简单的功能,可以将选定的对象 ID post 存储到会话存储中,它与数字一样工作良好,但是当我将其更改为单词时,它不起作用,知道为什么以及如何修复它吗?
我认为这很好,因为它们都是字符串,正如您在底部看到我的函数解析 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"
}]
};
$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);
}
}
});
我在检查器中遇到错误
SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse ()
在 $scope
$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"
}]
}]
您正在解析的 ID 不是 JSON,这就是您收到错误的原因。
事实上,整个范围对象只是一个 JavaScript 对象,而不是 JSON(这是一种文本格式)。
您获得的 ID 是字符串 "Roadside",但如果它是 JSON 字符串,它看起来类似于 "{ \"key\": \"value\" }”。它缺少左大括号的事实告诉解析器它是错误的。