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

Add before and after compile hooks #18

Open
wants to merge 7 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 1.3.0

* Added hooks before and after compile

### 1.2.1

* Change strict angular dependency to `~1.x`
Expand Down
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Add dependency to your app module

* `'angular-bind-html-compile'`

## Usage
## Usage
`ng-bind-html`:
```
<div ng-bind-html="data.content"></div>
Expand All @@ -28,6 +28,34 @@ If the `data.content` contained a directive, it would not be compiled.
<div bind-html-compile="data.content"></div>
```

Additionally, bind-html-before-compile and/or bind-html-after-compile
may be specified to execute code before of after the compile. E.g:

```
<div bind-html-compile="data.content"
bind-html-before-compile="beforeCompile"
bind-html-after-compile="afterCompile"></div>
```

And in your controller code:
```
...
function beforeCompile(element) {
// do something, e.g add angular attributes and directives to
// the HTML that was bound before it gets compiled
angular.element("input[name=someField]").attr("ng-model", "data.someField");
}
function afterCompile(element) {
// do something, e.g. toggle visibility back on if you had
// hidden the div while changing the HTML content
}
```

Example Plunkers:

* [Example of before and after compile hooks](http://plnkr.co/edit/f4LobH?p=preview)


## Development
* Contributions welcome - Create an issue to discuss proposed changes and additions
* All contributions should be done in branches and submitted as pull requests.
Expand Down
27 changes: 22 additions & 5 deletions angular-bind-html-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@
module.directive('bindHtmlCompile', ['$compile', function ($compile) {
return {
restrict: 'A',
scope: {
beforeCompile: '&bindHtmlBeforeCompile',
afterCompile: '&bindHtmlAfterCompile'
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 we want to isolate the scope here.

},
link: function (scope, element, attrs) {
scope.$watch(function () {
return scope.$eval(attrs.bindHtmlCompile);
scope.$parent.$watch(function () {
return scope.$parent.$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());

// beforeCompile hook
var beforeCompile = scope.beforeCompile();
if (angular.isFunction(beforeCompile)) {
beforeCompile(element);
}

// If scope is provided use it, otherwise use parent scope
var compileScope = scope;
var compileScope = scope.$parent;
if (attrs.bindHtmlScope) {
compileScope = scope.$eval(attrs.bindHtmlScope);
compileScope = scope.$parent.$eval(attrs.bindHtmlScope);
}
$compile(element.contents())(compileScope);

// After compile hook
var afterCompile = scope.afterCompile();
if (angular.isFunction(afterCompile)) {
afterCompile(element);
}
});
}
};
};
}]);
}(window.angular));
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-bind-html-compile",
"version": "1.2.1",
"version": "1.3.0",
"dependencies": {},
"devDependencies": {
"grunt": "~0.4.1",
Expand Down