车把JS正在将HTML作为字符串处理

时间:2018-09-02 14:26:58

标签: javascript handlebars.js

我正在将HTML内容作为字符串提供给带有双花括号{{}}的handlebars变量,希望将其作为html处理,但最终看到原始的html代码。下面是我的实现。请帮忙。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <script type="text/x-handlebars-template" id="temp">
        <div>
            hey there... {{ somedata }}
        </div>
    </script>

    <div id="thedata"></div>

    <script src="handlebar.js"></script>
    <script>
        let rawTemplate = document.getElementById("temp").innerHTML;
        let compiledTemplate = Handlebars.compile(rawTemplate);
        let somedata = "<span>hello there</span>"
        let handlebarJsonData = {
            somedata: somedata,
        };
        let htmlData = compiledTemplate(handlebarJsonData);
        document.getElementById("thedata").innerHTML = htmlData;
    </script>

</body>
</html>

我得到的结果是

  

嘿,那里... <span>hello there</span>

但是,我想要

  

嘿...你好

请帮助

0 个答案:

没有答案