0

I have 2 database tables, letters and letterTemplates with an Angularjs frontend.

In letterTemplates.content column, I have a html template with an embedded angular expression like so:

<p> Dear {{letter.user.name}},</p>

And in the letters preview, I display the letterTemplate.content like so:

<div ng-bind-html="letterTemplate.content"></div>

which returns the following output:

Dear {{letter.user.name}},

How do I get the Angular expression to execute?


The solutions outlined in How to make ng-bind-html compile angularjs code relied on using hardcoded Angular templates, whereas I need to parse html and angular statements from an API response.

Community
  • 1
  • 1
Ryan.lay
  • 1,741
  • 2
  • 17
  • 30
  • 1
    possible duplicate of [How to make ng-bind-html compile angularjs code](http://stackoverflow.com/questions/19726179/how-to-make-ng-bind-html-compile-angularjs-code) – Sergiu Paraschiv Apr 27 '15 at 10:14

1 Answers1

0

Here's how I got it to work:

Step 1. Install this directive: https://github.com/incuna/angular-bind-html-compile

Step 2. Include the directive in the module.

angular.module("app", ["angular-bind-html-compile"])

Step 3. Use the directive in the template:

<div bind-html-compile="letterTemplate.content"></div>

Output =

Dear John,

For reference sake, here's the relevant directive:

(function () {
    'use strict';

    var module = angular.module('angular-bind-html-compile', []);

    module.directive('bindHtmlCompile', ['$compile', function ($compile) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                scope.$watch(function () {
                    return scope.$eval(attrs.bindHtmlCompile);
                }, function (value) {
                    element.html(value);
                    $compile(element.contents())(scope);
                });
            }
        };
    }]);
}());
Ryan.lay
  • 1,741
  • 2
  • 17
  • 30