From 66f051386e153f10bd2751a7cafb61404799adee Mon Sep 17 00:00:00 2001 From: Martin Probst Date: Tue, 8 Jan 2013 20:11:51 +0100 Subject: feat($compile): support modifying the DOM structure in postlink fn Support modifying the DOM structure in the post link function of a directive by creating a defensive copy of the node list, as opposed to a live DOM list. This is useful for directives to actually replace their entire DOM fragment, e.g. with the HTML fragment generated by a 3rd party component (Closure, Bootstrap ...). Fix the indentation of the compileNodes function (was one too little). --- src/ng/compile.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/ng/compile.js') diff --git a/src/ng/compile.js b/src/ng/compile.js index 5ad83e42..84d53e6d 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -394,10 +394,16 @@ function $CompileProvider($provide) { return linkFnFound ? compositeLinkFn : null; function compositeLinkFn(scope, nodeList, $rootElement, boundTranscludeFn) { - var nodeLinkFn, childLinkFn, node, childScope, childTranscludeFn; + var nodeLinkFn, childLinkFn, node, childScope, childTranscludeFn, i, ii, n; - for(var i = 0, n = 0, ii = linkFns.length; i < ii; n++) { - node = nodeList[n]; + // copy nodeList so that linking doesn't break due to live list updates. + var stableNodeList = []; + for (i = 0, ii = nodeList.length; i < ii; i++) { + stableNodeList.push(nodeList[i]); + } + + for(i = 0, n = 0, ii = linkFns.length; i < ii; n++) { + node = stableNodeList[n]; nodeLinkFn = linkFns[i++]; childLinkFn = linkFns[i++]; -- cgit v1.2.3