18

As I'm new to Angular JS I was wondering how could I load an external template and compile it with some data into the targeted div.

For instance I have this template :

<script type="text/ng-template">

    <img src="{{Thumb}}" />

<script>

The div that is supposed to contain the template :

<div data-ng-controller=" ... "></div>

The template is located somewhere in a folder /templates/test.php. Is there a build in way of doing the template loading like a directive would do and compile it against some data that would replace the key {{Thumb}} ( and many others of course ) ?

EDIT : What if I use $routes and load a template when I'm in the root of the website ? How could that be achieved ?

3 Answers 3

26

Using $templateRequest, you can load a template by it’s URL without having to embed it into your HTML page. If the template is already loaded, it will be taken from the cache.

app.controller('testCtrl', function($scope, $templateRequest, $sce, $compile){
    // Make sure that no bad URLs are fetched. If you have a static string like in this
    // example, you might as well omit the $sce call.
    var templateUrl = $sce.getTrustedResourceUrl('nameOfTemplate.html');

    $templateRequest(templateUrl).then(function(template) {
        // template is the HTML template as a string

        // Let's put it into an HTML element and parse any directives and expressions
        // in the code. (Note: This is just an example, modifying the DOM from within
        // a controller is considered bad style.)
        $compile($("#my-element").html(template).contents())($scope);
    }, function() {
        // An error has occurred here
    });
});

Be aware that this is the manual way to do it, and whereas in most cases the preferable way would be to define a directive that fetches the template using the templateUrl property.

Sign up to request clarification or add additional context in comments.

Comments

18

in Angular there's 2 ways of using template (at least 2 ways that i know about):

  • the first using an inline template (in the same file) with this syntax:

    <script type="text/ng-template">
        <img ng-src="{{thumb}}">
    </script>
    
  • the second one (what you want) is external template:

    <img ng-src="{{thumb}}">
    

so what you need to do is to remove the script part from your template and then use the ng-include in the wanted div like this:

<div ng-include="'templates/test.php'"></div>

need to have double quotes and single quotes to work. Hope this helps.

6 Comments

It seems to be working. The {{Thumb}} will be only replaced with a value if I define an controller and I set the $scope.Thumb to a value. Also, I get a 404 for the {{Thumb}} image source, can that be avoided ?
put an example on plnkr, so I can understand you better.
Well, take a look at the example they have : docs.angularjs.org/api/ng.directive:ngView ; I'm setting up the same thing right now, but I get redirected, probably because the route doesn't match the location I have ? I have it as $routeProvider.when('', { templateUrl : 'templates/vcard.php', controller : "VCardController" });
Please, set an example code on plnkr, here's a plnkr about the $routes thing look at line 30 on the app.js file.
For images you should use ngSrc: <img ng-src='Thumb'>
|
2

Let's say I have this index.html:

 <!doctype html> <html lang="en" ng-app="myApp">
        <body>
            <script src="tpl/ng.menu.tpl" type="text/ng-template"></script>   
            <mainmenu></mainmenu>       
            <script src="lib/angular/angular.js"></script>
            <script src="js/directives.js"></script>
        </body> 
</html>

And I have a template file "tpl/ng.menu.tpl" with only these 4 lines:

<ul class="menu"> 
    <li><a href="#/view1">view1</a></li>
    <li><a href="#/view2">view2</a></li>
</ul>

My directives mapping "js/directives.js":

angular.module('myApp',['myApp.directives']);
var myModule = angular.module('myApp.directives', []);

myModule.directive('mainmenu', function() {
    return { 
        restrict:'E',
        replace:true,
        templateUrl:'tpl/ng.menu.tpl'
    }
});

1 Comment

Any idea why Angular sees only the content inside the <script> tags and not its loaded file content specified with the src attribute?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.