Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new scope and destory it to avoid scope troubles #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion angular-bind-html-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,34 @@
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) {
// In case value is a TrustedValueHolderType, sometimes it
// 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);
});
}
Expand Down
2 changes: 1 addition & 1 deletion angular-bind-html-compile.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.