简体   繁体   中英

Cleanest way to create html & xml content in angularjs

I need to send HTML string inside XML to a POST REST service.

Currently I am using string concatenation to create XML's and HTML's like the following,

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. 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:

    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:

    var html = document.getElementById("parent_div").innerHTML; alert(html);

using above code you can use your html without writing it in JS code. Also, you can hide this if you don't want to show it on your page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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