diff --git a/angular-bind-html-compile.js b/angular-bind-html-compile.js index a8473d7..05cd784 100644 --- a/angular-bind-html-compile.js +++ b/angular-bind-html-compile.js @@ -7,6 +7,9 @@ return { restrict: 'A', link: function (scope, element, attrs) { + + // Here we will store wich is the previous compiled scope + var _previousCompiledScope = null; scope.$watch(function () { return scope.$eval(attrs.bindHtmlCompile); }, function (value) { @@ -14,11 +17,24 @@ // needs to be explicitly called into a string in order to // get the HTML string. element.html(value && value.toString()); + + // If we have a previousCompiled scope, just destroy it + if (_previousCompiledScope !== null) { + _previousCompiledScope.$destroy(); + } + // If scope is provided use it, otherwise use parent scope - var compileScope = scope; + var compileScope = null; + if (attrs.bindHtmlScope) { compileScope = scope.$eval(attrs.bindHtmlScope); + } else { + // Create new scope (We can delete it) + compileScope = scope.$new(false); } + + _previousCompiledScope = compileScope; + $compile(element.contents())(compileScope); }); } diff --git a/angular-bind-html-compile.min.js b/angular-bind-html-compile.min.js index 73a02a6..7d6750e 100644 --- a/angular-bind-html-compile.min.js +++ b/angular-bind-html-compile.min.js @@ -1 +1 @@ -!function(a){"use strict";var b=a.module("angular-bind-html-compile",[]);b.directive("bindHtmlCompile",["$compile",function(a){return{restrict:"A",link:function(b,c,d){b.$watch(function(){return b.$eval(d.bindHtmlCompile)},function(e){c.html(e&&e.toString());var f=b;d.bindHtmlScope&&(f=b.$eval(d.bindHtmlScope)),a(c.contents())(f)})}}}])}(window.angular); \ No newline at end of file +!function(a){"use strict";var b=a.module("angular-bind-html-compile",[]);b.directive("bindHtmlCompile",["$compile",function(a){return{restrict:"A",link:function(b,c,d){var e=null;b.$watch(function(){return b.$eval(d.bindHtmlCompile)},function(f){c.html(f&&f.toString()),null!==e&&e.$destroy();var g=null;g=d.bindHtmlScope?b.$eval(d.bindHtmlScope):b.$new(!1),e=g,a(c.contents())(g)})}}}])}(window.angular); \ No newline at end of file