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

家住魔仙堡 提交于 2019-12-25 07:11:59

问题


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?


回答1:


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);
        }
    };
});


来源:https://stackoverflow.com/questions/23293620/angularjs-how-to-handle-a-popup-with-a-directive-so-that-data-comes-from-contro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!