Brittany Rutherford Brittany Rutherford - 1 month ago 5
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

ng-include
or any other directive as I don't really have an html to play with, I have only JavaScript file with data rendered from
REST
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

I think you should use directive and as James point out use the $compile. You can look some doc https://docs.angularjs.org/guide/compiler

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.

  $http({
    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
  }); 
Comments