简体   繁体   English

在angularjs中创建html和xml内容的最干净方法

[英]Cleanest way to create html & xml content in angularjs

I need to send HTML string inside XML to a POST REST service. 我需要将XML内的HTML字符串发送到POST REST服务。

Currently I am using string concatenation to create XML's and HTML's like the following, 目前,我正在使用字符串连接来创建XML和HTML,如下所示,

var html  = " <div style='some style'>... append model data from the response of some services ... </div> ";
    html += " <div>  ... append model data from the response of some services ...</div> ";

var xml  = "<?xml version='1.0' encoding='utf-8'?>";
    xml += "<root><data>";
    xml += "<![CDATA["+     html      +"]]>";
    xml += "</data></root>";

return     $http.post(url,xml,{headers : {'Content-Type': 'application/atom+xml'}});

It looks really ugly as the real html content is huge. 由于实际的html内容非常庞大,因此看起来非常难看。 Which is the cleanest way to achieve this except string concatenation? 除了字符串串联,哪种方法最干净?

You can do following: 您可以执行以下操作:

  1. Put your html code in body tag under any specific tag like div with some id or class (I have used id ) example: 将您的html代码放在body标签下的任何特定标签下,例如div,并带有一些idclass (我使用过id )的示例:

    The content of the document...... 文件内容……

    ... append model data from the response of some services ... ... append model data from the response of some services ... 从某些服务的响应中追加模型数据从某些服务的响应中追加模型数据

  2. Now in javascript you can do like below: 现在,在javascript中,您可以执行以下操作:

    var html = document.getElementById("parent_div").innerHTML; var html = document.getElementById(“ parent_div”)。innerHTML; alert(html); 警报(HTML);

using above code you can use your html without writing it in JS code. 使用以上代码,您可以使用html而不用JS代码编写它。 Also, you can hide this if you don't want to show it on your page. 另外,如果您不想在页面上显示它,可以将其隐藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM