Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular ng-bind-html and directive within it

Plunker Link

I have a element which I would like to bind html to it.

<div ng-bind-html="details" upper></div>

That works. Now, along with it I also have a directive which is bound to the bound html:

$scope.details = 'Success! <a href="#/details/12" upper>details</a>'

But the directive upper with the div and anchor do not evaluate. How do I make it work?

like image 546
Amitava Avatar asked Jul 02 '13 04:07

Amitava


People also ask

What is Ng-bind-HTML in AngularJS?

The ng-bind-html directive is a secure way of binding content to an HTML element. When you are letting AngularJS write HTML in your application, you should check the HTML for dangerous code. By including the "angular-sanitize.

Does ng-bind bind the application data to HTML tags in AngularJS?

ng-bind directive binds the AngularJS Application data to HTML tags. ng-bind updates the model created by ng-model directive to be displayed in the html tag whenever user input something in the control or updates the html control's data when model data is updated by controller.

Which directive link AngularJS with HTML?

AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.


2 Answers

I was also facing this problem and after hours searching the internet I read @Chandermani's comment, which proved to be the solution. You need to call a 'compile' directive with this pattern:

HTML:

<div compile="details"></div> 

JS:

.directive('compile', ['$compile', function ($compile) {     return function(scope, element, attrs) {         scope.$watch(             function(scope) {                 // watch the 'compile' expression for changes                 return scope.$eval(attrs.compile);             },             function(value) {                 // when the 'compile' expression changes                 // assign it into the current DOM                 element.html(value);                  // compile the new DOM and link it to the current                 // scope.                 // NOTE: we only compile .childNodes so that                 // we don't get into infinite loop compiling ourselves                 $compile(element.contents())(scope);             }         );     }; }]) 

You can see a working fiddle of it here

like image 112
vkammerer Avatar answered Oct 19 '22 11:10

vkammerer


Thanks for the great answer vkammerer. One optimization I would recommend is un-watching after the compilation runs once. The $eval within the watch expression could have performance implications.

    angular.module('vkApp')   .directive('compile', ['$compile', function ($compile) {       return function(scope, element, attrs) {           var ensureCompileRunsOnce = scope.$watch(             function(scope) {                // watch the 'compile' expression for changes               return scope.$eval(attrs.compile);             },             function(value) {               // when the 'compile' expression changes               // assign it into the current DOM               element.html(value);                // compile the new DOM and link it to the current               // scope.               // NOTE: we only compile .childNodes so that               // we don't get into infinite loop compiling ourselves               $compile(element.contents())(scope);                // Use un-watch feature to ensure compilation happens only once.               ensureCompileRunsOnce();             }         );     }; }]); 

Here's a forked and updated fiddle.

like image 30
user3075469 Avatar answered Oct 19 '22 11:10

user3075469