-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathangular-bind-html-compile.js
More file actions
44 lines (39 loc) · 1.7 KB
/
angular-bind-html-compile.js
File metadata and controls
44 lines (39 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(function (angular) {
'use strict';
var module = angular.module('angular-bind-html-compile', []);
module.directive('bindHtmlCompile', ['$compile', function ($compile) {
return {
restrict: 'A',
scope: {
beforeCompile: '&bindHtmlBeforeCompile',
afterCompile: '&bindHtmlAfterCompile'
},
link: function (scope, element, attrs) {
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.$parent;
if (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));