Brittany Rutherford Brittany Rutherford - 2 years ago 126
AngularJS Question

Fetching HTML in Angular

I am getting started with Angular, and I want to get HTML template from a file using JavaScript, I don't want to use

or any other directive as I don't really have an html to play with, I have only JavaScript file with data rendered from
service and I need to send HTML output to the page I am in, so I don't really have any existing elements in the DOM.

So how do I get HTML from another file and use it just from JavaScript, in an Angular way?

Answer Source

I think you should use directive and as James point out use the $compile. You can look some doc

If for any reason you don't want to do that, I can put an exemple of a really BAD angular code I use on my app to show the CGU (an HTML from another domain).

Note that we use jQuery, and that's not a good thing in Angular world.

    method: 'GET',
    url: 'URL_HTML'
  }).success(function (data, status, headers, config) {
    var html_from_server = $.parseHTML(data);
    data = $(html_from_server.find('#container'));//look for the content of an ID.
    $('#content_cgu').html(data.html());// get an element on our page and replace the html content
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download