Issue
I have a javascript code created server side and passed to the client as a string by a rest request. I am trying to execute the code retrieved. Any help would be appreciated.
Server side. The javascript code stored in a string.
(function() {
function createChart(selector) {
$(selector).highcharts({
chart: {
type: 'column',
},
xAxis: {
crosshair: true,
type: "category"
},
yAxis: {
min: 0,
title: {
text: null
}
},
series: [{
data: datas
}],
});
}
function render() {
createChart('#chart');
}
return {
render: render
}
}())
Client side (angular js)
.controller('ChartController', ['$scope', 'charts',function ($scope, charts) {
var test = charts.data;//contains the javascript code send by a rest request
eval(test);
test.render();
}])
The execution of the script in the client side returns "test.render is not a function" in chrome
Any advices ?
Thank you very much
Solution
Use:
var result = eval(test);
result.render();
Because your test
is a string. The result
of eval(test)
is a function.
It is always not recommended to use eval
. But if you really need it, use angular $eval
instead:
If you want to eval() an Angular expression yourself, use the $eval() method.
Answered By - Joy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.