JSON 存储函数并删除引号的问题
JSON issue to store a function and remove the quotes
我尝试从我的服务器创建一个 json 并在客户端 angular 中检索 JSON。但是我的json是一个函数。
我在服务器中创建这个JSON:
var header = {};
var editor = 'function (container, options) {jsdd.appendTo(container); $compile(editor)($scope); editor.css("visibility", "visible")};';
header['editor'] = editor;
在客户端中,我像这样检索 json:
editor: "function (container, options) {jsdd.appendTo(container); $compile(editor)($scope); editor.css("visibility", "visible")};"
但是它不起作用,因为我想删除引号并得到这个:
editor: function (container, options) {jsdd.appendTo(container); $compile(editor)($scope); editor.css("visibility", "visible")};
我该怎么做?
更新
我尝试从服务器发送的json中获取的结果是这样的:
{
title: "category",
field: "category",
editor: function (container, options) {
var editor = $('<input kendo-drop-down-list required k-data-text-field="\'valueen\'" k-data-value-field="\'id\'" k-data-source=\"mapDSource.get(\'category\')\" data-bind="value:' + options.field + '"/>')
.appendTo(container);
$compile(editor)($scope);
editor.css("visibility", "visible");
}
, template: "{{getName(dataItem, 'category', 'id', 'valueen')}}"
, type: 'string',
editable: true,
width: 250
},
{
title: 'keyen',
type: 'string',
field: 'keyen',
editable: true,
validation: {
required: true
}, ....
除作为函数的编辑器参数外,所有工作正常,所以我正在做的是以下工作:
var table = {};
table.type = "referential";
gridService.loadTable(table).success(function (data) {
if(data.header)
{
var i;
for(i in data.header)
{
var editor = data.header[i]['editor'];
if(editor)
{
data.header[i]['editor'] = new Function("container", "options", editor);
}
}
}
....
我想 $parse 就是您要找的。我可能遗漏了一些上下文,但这似乎是一个奇怪的用例。你为什么不直接在http请求中得到一个.js文件,而不是在JSON对象业务中得到这个字符串化的JS?
Althgough eval
不是一个好主意,它可能是唯一的选择(假设您不更改当前的实现,我觉得您应该这样做)。
h['editor'] = eval('('+editor+')');
感觉不好推荐eval
。
作为旁注,避免 eval
的一种快速方法是 return 仅 json 字符串中的函数主体(大括号之间的部分):
var editor = 'jsdd.appendTo(container); $compile(editor)($scope); editor.css("visibility", "visible");'
然后像这样定义 header['editor']
:
header['editor'] = new Function("container","options",editor);
我尝试从我的服务器创建一个 json 并在客户端 angular 中检索 JSON。但是我的json是一个函数。
我在服务器中创建这个JSON:
var header = {};
var editor = 'function (container, options) {jsdd.appendTo(container); $compile(editor)($scope); editor.css("visibility", "visible")};';
header['editor'] = editor;
在客户端中,我像这样检索 json:
editor: "function (container, options) {jsdd.appendTo(container); $compile(editor)($scope); editor.css("visibility", "visible")};"
但是它不起作用,因为我想删除引号并得到这个:
editor: function (container, options) {jsdd.appendTo(container); $compile(editor)($scope); editor.css("visibility", "visible")};
我该怎么做?
更新
我尝试从服务器发送的json中获取的结果是这样的:
{
title: "category",
field: "category",
editor: function (container, options) {
var editor = $('<input kendo-drop-down-list required k-data-text-field="\'valueen\'" k-data-value-field="\'id\'" k-data-source=\"mapDSource.get(\'category\')\" data-bind="value:' + options.field + '"/>')
.appendTo(container);
$compile(editor)($scope);
editor.css("visibility", "visible");
}
, template: "{{getName(dataItem, 'category', 'id', 'valueen')}}"
, type: 'string',
editable: true,
width: 250
},
{
title: 'keyen',
type: 'string',
field: 'keyen',
editable: true,
validation: {
required: true
}, ....
除作为函数的编辑器参数外,所有工作正常,所以我正在做的是以下工作:
var table = {};
table.type = "referential";
gridService.loadTable(table).success(function (data) {
if(data.header)
{
var i;
for(i in data.header)
{
var editor = data.header[i]['editor'];
if(editor)
{
data.header[i]['editor'] = new Function("container", "options", editor);
}
}
}
....
我想 $parse 就是您要找的。我可能遗漏了一些上下文,但这似乎是一个奇怪的用例。你为什么不直接在http请求中得到一个.js文件,而不是在JSON对象业务中得到这个字符串化的JS?
Althgough eval
不是一个好主意,它可能是唯一的选择(假设您不更改当前的实现,我觉得您应该这样做)。
h['editor'] = eval('('+editor+')');
感觉不好推荐eval
。
作为旁注,避免 eval
的一种快速方法是 return 仅 json 字符串中的函数主体(大括号之间的部分):
var editor = 'jsdd.appendTo(container); $compile(editor)($scope); editor.css("visibility", "visible");'
然后像这样定义 header['editor']
:
header['editor'] = new Function("container","options",editor);