forked from incuna/angular-bind-html-compile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-bind-html-compile.js
More file actions
46 lines (44 loc) · 2.12 KB
/
angular-bind-html-compile.js
File metadata and controls
46 lines (44 loc) · 2.12 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
45
46
(function (angular) {
'use strict';
var module = angular.module('angular-bind-html-compile', []);
module.directive('bindHtmlCompile', ['$templateRequest', '$compile', function ($templateRequest, $compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(function () {
return scope.$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());
// If scope is provided use it, otherwise use parent scope
var compileScope = scope;
if (attrs.bindHtmlScope) {
compileScope = scope.$eval(attrs.bindHtmlScope);
}
$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;}
});
}
});
}
};
}]);
}(window.angular));