angular load template from url and compile inside div

All we need is an easy explanation of the problem, so here it is.

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 ?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.

Method 2

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.

Method 3

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" rel="nofollow noreferrer noopener">view1</a></li>
    <li><a href="#/view2" rel="nofollow noreferrer noopener">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'
    }
});

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply