In Angular, you can inject the $compile service, and you can compile things on the fly. Like this:
let compiledElement = $compile('<my-elem></my-elem>')(myScope);
So what I want to know is if it is possible to save the result of the first call to $compile so that it can be reused. Something like this:
let templateFunction = $compile('<my-elem></my-elem>');
cacheTemplateFunction(templateFunction);
let compiledElement = templateFunction(myScope);
Practically, here is what my code looks like:
const cachedTemplateFunction = {};
function compileRawHtml(html, scope){
let compiledEl;
if(cachedTemplateFunction[html]){
let tempFunc = cachedTemplateFunction[html];
compiledEl = tempFunc(scope)
} else {
let tempFunc = $compile(html);
cachedTemplateFunction[html] = tempFunc;
compiledEl = tempFunc(scope);
}
}
So the first time through with that template, it will compile and cache the template function. Then the second time through it reuses it. When I call this, I get an error about childNodes of undefined. So am I to assume that you can't reuse the template function that is returned from the $compile call?