In the application I am working on, a service supplies the app with a json, containing HTML. I output the HTML in a template like this:
<div ng-bind-html="renderHtml(widget.article.body)"></div>
This HTML may contain custom directives, like an inline-chart:
directiveModule.directive('inlineChart', function(){
return {
restrict: 'A',
scope: { inline-widget:'=elem' },
template: '<p ng-bind="inline-widget.blah"></p>'
};
});
If I use the directive normally in a template, everything works fine, but it seems it is not being touched when used in ng-bing-html with the renderHtml function.
Any suggestions how this could be realised are very much appreciated!
See https://stackoverflow.com/a/31880192/1345244
Add this directive angular-bind-html-compile
Use it like this :
Really easy :)
You need to import the
ngSanitize
module and use the$sce
service. It should look something like this:In short, the
$sce
service will mark the html as trusted. You can find documentation hereEDIT: I realize I may have not answered the question. It seems that you're asking about binding scope variables to the directive that is rendered within your directive? In order to get elements to compile properly, you're going to have to use the
$compile
service and change your logic up a bit. First, the template:Then the directive:
You can find the compile documentation here. Hopefully this is a more comprehensive answer to what you're looking for.
EDIT 2: For clarification, the directive should look like this on the page:
The
place-directive-here
class is just a handle for the directive to find your<p>
tag and render inside of it. However, if angular is not concerned with the html that is rendered inside ofmy-directive
, the first solution that I provided should work just fine and the template property ofmy-directive
should be:An example of Angular directive having attributes with HTML tags
View working example over Plunker
This example shows how to create
md-card
withindirective
by passingtitle
andcontent
and content having anchor tag<a>
Use
$sce.trustAsHtml
withinlink
as$sce
works when it is bound toscope
HTML
JS