Search code examples
angularjsangularjs-directiveng-view

angularjs: how to handle a popup with a directive so that data comes from controller model


Suppose I have this tag in a template URL, into which HTML coming from the model is injected (HTML is basically <li> bullet points):

<div ng-bind-html="accident.description.impact"></div>

A portion of the model is as follows:

"cause": 

"<ul>\n\
    <li>\n\
    <div><span inline-popover \n\
        popover-html=\"Taper pour ouvrir la vue détaillée\" \n\
        popover-placement=\"bottom\" \n\
        popover-label=\"Larve de taupin\">Larve de taupin</span></div>\n\
    </li>\n\
    <li>Semences en cours de germination</li>\n\
</ul>",

And as you can see, some <li> contains tags that enables for opening a popup.

The issue I have, is that the span is cut off from his attributes, once injected in the partial.

Any idea?


Solution

  • The problem here is: the ng-bind-html does simply output the content of the variable. What you need here is a component, that $compiles the code, to make additional directives work.

    <div ng-compile-html="accident.description.impact"></div>
    

    Javascript:

    directives.directive("ngCompileHtml", function ($http, $compile)
    {
        return {
            restrict: "A",
            scope: {
                    "ngCompileHtml": "="            
            },
            link: function (scope, element)
            {
                var template = angular.element(scope.ngCompileHtml);
                $compile(template.contents())(scope);
                $(element).append(template);
            }
        };
    });