diff --git a/CHANGELOG.md b/CHANGELOG.md index e8bcf61..2d9b364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 1.3.0 + +* Added hooks before and after compile + ### 1.2.1 * Change strict angular dependency to `~1.x` diff --git a/README.md b/README.md index f52c38c..79ee534 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Add dependency to your app module * `'angular-bind-html-compile'` -## Usage +## Usage `ng-bind-html`: ```
@@ -28,6 +28,34 @@ If the `data.content` contained a directive, it would not be compiled. ``` +Additionally, bind-html-before-compile and/or bind-html-after-compile +may be specified to execute code before of after the compile. E.g: + +``` + +``` + +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. diff --git a/angular-bind-html-compile.js b/angular-bind-html-compile.js index a8473d7..361f4a1 100644 --- a/angular-bind-html-compile.js +++ b/angular-bind-html-compile.js @@ -6,22 +6,39 @@ module.directive('bindHtmlCompile', ['$compile', function ($compile) { return { restrict: 'A', + scope: { + beforeCompile: '&bindHtmlBeforeCompile', + afterCompile: '&bindHtmlAfterCompile' + }, 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)); diff --git a/angular-bind-html-compile.min.js b/angular-bind-html-compile.min.js index 73a02a6..42dc5a0 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(b){return{restrict:"A",scope:{beforeCompile:"&bindHtmlBeforeCompile",afterCompile:"&bindHtmlAfterCompile"},link:function(c,d,e){c.$parent.$watch(function(){return c.$parent.$eval(e.bindHtmlCompile)},function(f){d.html(f&&f.toString());var g=c.beforeCompile();a.isFunction(g)&&g(d);var h=c.$parent;e.bindHtmlScope&&(h=c.$parent.$eval(e.bindHtmlScope)),b(d.contents())(h);var i=c.afterCompile();a.isFunction(i)&&i(d)})}}}])}(window.angular); \ No newline at end of file diff --git a/package.json b/package.json index f8f9230..67ce136 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-bind-html-compile", - "version": "1.2.1", + "version": "1.3.0", "dependencies": {}, "devDependencies": { "grunt": "~0.4.1",