Insert HTML into view from AngularJS controller

前端 未结 18 1782
Happy的楠姐
Happy的楠姐 2020-11-21 06:00

Is it possible to create an HTML fragment in an AngularJS controller and have this HTML shown in the view?

This comes from a requirement to turn an

18条回答
  •  既然无缘
    2020-11-21 06:47

    Here's a simple (and unsafe) bind-as-html directive, without the need for ngSanitize:

    myModule.directive('bindAsHtml', function () {
        return {
            link: function (scope, element, attributes) {
                element.html(scope.$eval(attributes.bindAsHtml));
            }
        };
    });
    

    Note that this will open up for security issues, if binding untrusted content.

    Use like so:

提交回复
热议问题