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

Support for template url #24

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# angular-bind-html-compile
This repo contains a bower package that provides an angular directive which can be passed trusted html with angular template content to evaluate.

The `bind-html-compile` directive allows for HTML containing directives to be compiled.
The `bind-html-compile` directive allows for HTML containing **custom** directives to be compiled.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this change is needed, since it doesn't matter if these are custom or angular core directives

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"custom" taken out


You should only use this directive where the content is coming from a trusted
source.
Expand All @@ -16,16 +16,25 @@ Add dependency to your app module
* `'angular-bind-html-compile'`

## Usage
`ng-bind-html`:
Just like the standard `ng-bind-html`, `bind-html-compile` goes like this:
```
<div ng-bind-html="data.content"></div>
<div bind-html-compile="data.content"></div>
```

If the `data.content` contained a directive, it would not be compiled.
(Unlike the standard `ng-bind-html`, `bind-html-compile` compiles directives, and even those custom ones.)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again adding the information about "custom directives" is probably too much

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"custom" taken out


`bind-html-compile`:
`template-url` attribute can be used to load a template file:
```
<div bind-html-compile="data.content"></div>
<div bind-html-compile template-url="data.templateUrl"></div>
```
or a static path:
```
<div bind-html-compile template-url="'partials/myTemplate.html'"></div>
```

Also both attributes can be used together for loading a "piece of html code" as well as a "template file":
```
<div bind-html-compile="data.content" template-url="data.templateUrl"></div>
```

## Development
Expand Down
21 changes: 20 additions & 1 deletion angular-bind-html-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var module = angular.module('angular-bind-html-compile', []);

module.directive('bindHtmlCompile', ['$compile', function ($compile) {
module.directive('bindHtmlCompile', ['$templateRequest', '$compile', function ($templateRequest, $compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
Expand All @@ -21,6 +21,25 @@
}
$compile(element.contents())(compileScope);
});
scope.$watch(function () {
return scope.$eval(attrs.templateUrl);
}, function (src) {
if (src) {
// set the 2nd param to true to ignore the template request error so that the inner
// contents and scope can be cleaned up.
$templateRequest(src, true).then(function (html) {
var tpl = angular.element(html);
element.append(tpl);
var compileScope = scope;
if (attrs.templateUrl) {
compileScope = scope.$eval(attrs.templateUrl);
}
$compile(tpl)(compileScope);
}, function () {
if (scope.$$destroyed) {return;}
});
}
});
}
};
}]);
Expand Down