如何使用'javascript变量作为文本'?

时间:2022-02-21 00:36:56

Sorry for posting this again, but I can't find anything useful on the internet. I'm trying to create a calendar. I'm using symfony 3 and fullcalendar to create it. So in my twig class I've created a variable:

很抱歉再次发布此内容,但我在互联网上找不到任何有用的内容。我正在尝试创建一个日历。我正在使用symfony 3和fullcalendar来创建它。所以在我的twig类中我创建了一个变量:

{% set fff = "" %}

Into fff variable I add some text like {start: "2017-05-17", title: "Take my mom from airport"}, Then I pass the fff variable to JS:

进入fff变量我添加一些文本,如{start:“2017-05-17”,标题:“从机场带我的妈妈”},然后我将fff变量传递给JS:

<script>
    var allTasks = {{ fff|json_encode()|raw }};
</script>

Then if I want to add this task {start: "2017-05-17", title: "Take my mom from airport"},, allTasks variable, to the calendar. So I've created a function:

然后,如果我想添加此任务{start:“2017-05-17”,标题:“从机场带我的妈妈”},allTask​​s变量,到日历。所以我创建了一个函数:

$(function(){
    $('#calendar').fullCalendar({
         events: [
             //there I should add tasks
             //f.e. {start: "2017-05-17", title: "Take my mom from airport"},
             //this code adds a task into my calendar 
         ],
     });
 });

When I put code like in the example it works fine, but when I try to do something like:

当我把代码放在例子中它工作正常,但当我尝试做类似的事情:

$(function(){
    $('#calendar').fullCalendar({
        events: [
            allTasks, //this variable is equal to "{start: "2017-05-17", title: "Take my mom from airport"},"
        ],
     });
 });

This gives me nothing. Even calendar disappear from my web. So the question is what I'm doing wrong? allTasks var prints out {start: "2017-05-17", title: "Take my mom from airport"},. I've tested it, but when I try to use it inside my function it doesn't work.

这没什么。甚至日历也从我的网络上消失了。所以问题是我做错了什么? allTask​​s var打印出{start:“2017-05-17”,标题:“从机场带我的妈妈”} ,.我已经测试了它,但当我尝试在我的功能中使用它时它不起作用。

Error log: [2017-05-15 11:40:36] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /[%7Bstart:'2017-05-17',title:'Take%20my%20mom%20from%20airport'%7D]" (from "http://127.0.0.1:8000/home")" at /home/david/task_manager/var/cache/dev/classes.php line 3497 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): No route found for \"GET /[%7Bstart:'2017-05-17',title:'Take%20my%20mom%20from%20airport'%7D]\" (from \"http://127.0.0.1:8000/home\") at /home/david/task_manager/var/cache/dev/classes.php:3497, Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0): at /home/david/task_manager/var/cache/dev/appDevDebugProjectContainerUrlMatcher.php:162)"} [] 如何使用'javascript变量作为文本'?如何使用'javascript变量作为文本'?

错误日志:[2017-05-15 11:40:36] request.ERROR:未捕获PHP异常Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException:“找不到”GET / [%7Bstart:'2017-05-17的路由',title:'从%20airport'%7D获取%20my%20mom%20'“(来自”http://127.0.0.1:8000/home“)”at / home / david / task_manager / var / cache / dev / classes.php line 3497 {“exception”:“[object](Symfony \\ Component \\ HttpKernel \\ Exception \\ NotFoundHttpException(code:0):找不到\”GET / [%7Bstart:'2017-05 -17',标题:'取%20my%20mom%20来自%20airport'%7D] \“(来自\”http://127.0.0.1:8000 / home \“)/ home / david / task_manager / var / cache / dev / classes.php:3497,Symfony \\ Component \\ Routing \\ Exception \\ ResourceNotFoundException(code:0):at /home/david/task_manager/var/cache/dev/appDevDebugProjectContainerUrlMatcher.php:162)" } []

1 个解决方案

#1


0  

You can try this :

你可以试试这个:

<script>
    var allTasks = '{{ fff|json_encode()|raw }}'; // make sure that fff return {'start': '2017-05-17', 'title': 'Take my mom from airport'}
</script>

$(function(){
    $('#calendar').fullCalendar({
        events: [
            JSON.parse(allTasks)
        ],
     });
 });

#1


0  

You can try this :

你可以试试这个:

<script>
    var allTasks = '{{ fff|json_encode()|raw }}'; // make sure that fff return {'start': '2017-05-17', 'title': 'Take my mom from airport'}
</script>

$(function(){
    $('#calendar').fullCalendar({
        events: [
            JSON.parse(allTasks)
        ],
     });
 });