forked from marklagendijk/angular-recursion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-recursion.js
More file actions
executable file
·58 lines (56 loc) · 1.85 KB
/
angular-recursion.js
File metadata and controls
executable file
·58 lines (56 loc) · 1.85 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
47
48
49
50
51
52
53
54
55
56
57
58
/*
* An Angular service which helps with creating recursive directives.
* @author Mark Lagendijk
* @license MIT
*/
angular.module('RecursionHelper', []).factory('RecursionHelper', ['$compile', function($compile){
return {
/**
* Manually compiles the element, fixing the recursion loop.
* @param element
* @param [link] A post-link function, or an object with function(s) registered via pre and post properties.
* @returns An object containing the linking functions.
*/
compile: function(element, link, replace_existing){
// Normalize the link parameter
if(angular.isFunction(link)){
link = { post: link };
}
// Break the recursion loop by removing the contents
var contents = element.contents().remove();
var compiledContents;
return {
pre: (link && link.pre) ? link.pre : null,
/**
* Compiles and re-adds the contents
*/
post: function (scope, element, attributes, controller, transcludeFn) {
// Compile the contents
if(!compiledContents){
compiledContents = $compile(contents);
}
// Re-add the compiled contents to the element
var cloneAttachFn = function (clone) {
if (replace_existing) {
element.after(clone);
element.hide();
element = clone;
} else {
element.append(clone);
}
};
var options = {
parentBoundTranscludeFn: function (scope, cloneAttachFn, transcludeControllers, futureParentElement, scopeToChild) {
transcludeFn.call(null, scopeToChild, cloneAttachFn, /*futureParentElement.is('tr') ? futureParentElement.parent() :*/ futureParentElement);
}
};
compiledContents(scope, cloneAttachFn, options);
// Call the post-linking function, if any
if(link && link.post){
link.post.call(null, scope, element, attributes, controller, transcludeFn);
}
}
};
}
};
}]);