2

I'm working on an AngularJS 1.5 project with PhoneGap. Now I would like to write the HTML output to a textfile.

I was trying to use the innerHTML function, but this returns the wrong output, example this return:

<html>
  <body>Hi {{ Name }}</body>
</html>

What I really want to save:

<html>
  <body>Hi Developer</body>
</html>

Does someone has the solution? Thanks!

2 Answers2

0

This is going to be a bit more complicated than what it seems at first glance. Angular does not have some sort of output buffering you simply can get a hold of. Your best bet will be to to save the rendered markup using your browser. In Firefox you can use View Source » View Generated Source. In Chrome you find a similar option under the tools menu.

Or if you want to go down the code road, here is a response from one of the Angular devs, Misko Hevery:

We have not done this, but if you feel up to it here is what you can try

  • get node.js running
  • install of the many DOM implementations for node
  • Get your angular page running inside the node.js.
  • Get a hold of the $browser service and call notifyWhenNoOutstandingRequests() this will tell you when the page finished rendering
  • Do document.html() to get rendered HTML

[...]

subZero
  • 5,056
  • 6
  • 31
  • 51
  • Thanks for answer. The only problem is that I'm using PhoneGap in my project. So I can't use the above solution. I would like to save the html to generate a PDF of the page... – user2986666 Nov 13 '13 at 08:16
  • And what about computed CSS styles? How to save them together with html? – Stanislav Berkov Mar 24 '14 at 17:53
0

If you're actually using angular then you should be able to use innerHTML after the binding is done. (fiddle)

<div ng:controller="CombinedCtrl" id="combined">
    X: {{x}}, Y: {{y}}
    <button ng-click="showHtml()">Show HTML</button>
</div>

JS:

    $scope.showHtml = function() {
        alert('HTML IS:\r\n' + document.getElementById('combined').innerHTML);
    };
Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113