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 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
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
(However, unlike the standard `ng-bind-html`, `bind-html-compile` compiles directives!)

`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