Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate HTML string from AngularJS template string

I have angularjs template as a string including "ng-repeat" and other directives. I want to compile it in the controller to produce the result HTML as string.

Example on what I want to apply in Angular:

Input:
-------
var template = '<div ng-repeat="item in items">{{item.data}}</div>';

Output:
--------
var result = '<div>1</div><div>2</div><div>3</div><div>4</div>';

I want this to be done in the controller I've and I tried the following:

var template = '<div ng-repeat="item in items">{{item.data}}</div>';
var linkFunction = $compile(template);
var result = linkFunction($scope);

console.log(result); // prints the template itself!

Thanks!

like image 639
Kareem Elshahawy Avatar asked Oct 15 '14 15:10

Kareem Elshahawy


People also ask

Can you use template strings in HTML?

In ES6, we can now use template strings. I like to use these multiline strings when I'm creating HTML markup as a string. You can use backticks here, and use the object properties as variables. These look familiar because they're in the same format as I've been using in some of the previous posts.

How do you create a string in HTML?

You can create a String instance using plain text or HTML, for example both of these statements are valid: // From plain text var helloWorld = new HTMLString. String('Hello World'); // From HTML var helloWorldBold = new HTMLString. String('Hello World');

How do you write template literals in HTML?

Sanitising HTML with a Tagged Template Literal You can do that in a template literal tag using a library like html-parser2. We want to have two types of input into the placeholder, raw text strings which needs sanitising and safe HTML which is either authored by us or has been put through the sanitiser.

What is template strings?

Template strings are a powerful feature of modern JavaScript released in ES6. It lets us insert/interpolate variables and expressions into strings without needing to concatenate like in older versions of JavaScript. It allows us to create strings that are complex and contain dynamic elements.


2 Answers

Give this a whirl:

var template = angular.element('<div ng-repeat="item in items">{{item.data}}</div>');
var linkFunction = $compile(template);
var result = linkFunction($scope);
$scope.$apply();

console.log(template.html());
like image 74
Fordio Avatar answered Sep 19 '22 13:09

Fordio


For this purpose I made myself a directive couple of projects ago:

angular.module('myApp')

.directive('ngHtmlCompile', ["$compile", function ($compile) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            scope.$watch(attrs.ngHtmlCompile, function (newValue, oldValue) {
                element.html(newValue);
                $compile(element.contents())(scope);
            });
        }
    }
}]);

and then for example:

<div ng-html-compile='<div ng-repeat="item in items">{{item.data}}</div>'></div>

or even the string can be dynamic:

<div ng-repeat="item in items" ng-html-compile="item.template">
</div>
like image 42
Ali Habibzadeh Avatar answered Sep 23 '22 13:09

Ali Habibzadeh