6

I am trying to render a piece of html, available on a dynamic route, the route is fetched via a $http.get() call, it returns a piece of html,

Just to give an example I try to load this html partial:

<h1>{{ pagetitle }}</h1>
this is a simple page example

I made a small fiddle, to mock the problem, but for the simplicity i left the http call out, and just added the html in a string on the scope.

The controller is:

function Ctrl($scope) {
    $scope.data = {
        view: "<h1>whaaaaa</h1>"        
    }; 
}

The page html is this:

<div ng-app="">
  <div ng-controller="Ctrl">
    <div ng-include src="data.view"></div>
  </div>
</div>

The problem is that it does not add the string into the html file (ng-include), but it makes a http call to a url made of that string aparently.

So Is it not possible to just enter a string into an include? if not, what is the proper way to make an http call to a dynamic url, and enter the returned url into the page.

You can play with it in JSFiddle.

trejder
  • 17,148
  • 27
  • 124
  • 216
Sander
  • 13,301
  • 15
  • 72
  • 97
  • 1
    Check your keyboard. `Shift` key specific. There may be some misfuction around it. You start most sentences with a small letter, while you should use capital one. Someone must edit your question and answers and fix that. This is a quite boring task after all. Maybe you can purchase yourself a brand new keyboard, with fully functioning `Shift` key, as your very own Christmas gift? – trejder Dec 13 '14 at 20:16

4 Answers4

13

You can add static hooks to the template cache, so let's say you have a service that gets the template:

myApp.service('myTemplateService', ['$http', '$templateCache', function ($templateCache) {
  $http(/* ... */).then(function (result) {
    $templateCache.put('my-dynamic-template', result);
  });

  /* ... */
}]);

Then, you can do:

<div include src=" 'my-dynamic-template' "></div>

NOTE reference name must be wrapped in single quotes, this always bytes me.... NOTE

Do note that you'll have to have assigned it to the template cache before angular tries to resolve the url.

EDIT:

If the dynamic URL logic is simple enough, you can also use a conditional in the ng-include, e.g.

<div ng-include="isTrue() && 'template1.html' || 'template2.html'"
GnrlBzik
  • 3,358
  • 5
  • 27
  • 37
quinnirill
  • 814
  • 4
  • 6
  • 1
    This is what I wanted, I pull in my partial templates via AMD, and then the controller can assemble them into the $templateCache, and use ng-include="'myTpl" – httpete May 12 '16 at 17:08
  • 1
    Fantastic work around. To lazy load, I simply include a `ng-if` on the include and set the variable to true after populating the template. – Bryant Makes Programs May 26 '17 at 14:51
7

You can't do that with ng-include; reference:

ngInclude|src – {string} – angular expression evaluating to URL. If the source is a string constant, make sure you wrap it in quotes, e.g. src="'myPartialTemplate.html'".

Use directly ng-bind-html-unsafe like this:

<div ng-bind-html-unsafe="data.view"></div>

Demo

Creates a binding that will innerHTML the result of evaluating the expression into the current element. The innerHTML-ed content will not be sanitized! You should use this directive only if ngBindHtml directive is too restrictive and when you absolutely trust the source of the content you are binding to.

EpokK
  • 38,062
  • 9
  • 61
  • 69
5

You can use ng-bind-html-unsafe

If you do not trust the html which comes from your ajax request, you can also use ng-bind-html which needs the $sanitize service to work (DEMO).

<div ng-bind-html="data.view"></div>

To use that you need to include an extra js file .

angular.module('app', ['ngSanitize']);

And of course add the app to ng-app:

<div ng-app="app">
Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
1

ng-include expects a URL to an external HTML fragment, not an HTML string.

You want ng-bind-html-unsafe:

Updated demo.

André Dion
  • 21,269
  • 7
  • 56
  • 60
  • 3
    ng-bind-html-unsafe would work but it is only usefull when printing html, not html that again needs to be compiled with ng-binds and what not inside. however, i solved it by using the templatecache example from @quinnirill as that is a very clean solution without the need to extra compile the html. still thanks for the idea – Sander Aug 09 '13 at 15:13