From e783d06017d0722942e636a5a3783f449117b8da Mon Sep 17 00:00:00 2001 From: epic Date: Mon, 23 Jan 2017 10:42:10 +0100 Subject: [PATCH 1/2] Create new scope and destory it to avoid scope troubles --- angular-bind-html-compile.js | 21 ++++++++++++++++++++- angular-bind-html-compile.min.js | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/angular-bind-html-compile.js b/angular-bind-html-compile.js index a8473d7..5ba3448 100644 --- a/angular-bind-html-compile.js +++ b/angular-bind-html-compile.js @@ -7,6 +7,10 @@ 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 +18,26 @@ // 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 From 657d42ff020b1364c5f8a52f05aca0806e7fd39c Mon Sep 17 00:00:00 2001 From: epic Date: Mon, 23 Jan 2017 10:53:04 +0100 Subject: [PATCH 2/2] travis free --- angular-bind-html-compile.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/angular-bind-html-compile.js b/angular-bind-html-compile.js index 5ba3448..05cd784 100644 --- a/angular-bind-html-compile.js +++ b/angular-bind-html-compile.js @@ -8,9 +8,8 @@ restrict: 'A', link: function (scope, element, attrs) { - //Here we will store wich is the previous compiled scope + // Here we will store wich is the previous compiled scope var _previousCompiledScope = null; - scope.$watch(function () { return scope.$eval(attrs.bindHtmlCompile); }, function (value) { @@ -19,8 +18,8 @@ // get the HTML string. element.html(value && value.toString()); - //If we have a previousCompiled scope, just destroy it - if(_previousCompiledScope !== null) { + // If we have a previousCompiled scope, just destroy it + if (_previousCompiledScope !== null) { _previousCompiledScope.$destroy(); } @@ -29,15 +28,13 @@ if (attrs.bindHtmlScope) { compileScope = scope.$eval(attrs.bindHtmlScope); - } - else { - //Create new scope (We can delete it) + } else { + // Create new scope (We can delete it) compileScope = scope.$new(false); } _previousCompiledScope = compileScope; - $compile(element.contents())(compileScope); }); }