Skip to content

Instantly share code, notes, and snippets.

@guillaume86
Created February 28, 2014 15:16
Show Gist options
  • Save guillaume86/9272837 to your computer and use it in GitHub Desktop.
Save guillaume86/9272837 to your computer and use it in GitHub Desktop.
var $compileCache = function ($http, $templateCache, $compile) {
var cache = {};
return function (src, scope, cloneAttachFn) {
var compileFn = cache[src];
if (compileFn) {
compileFn(scope, cloneAttachFn);
} else {
$http.get(src, { cache: $templateCache }).success(function (response) {
var responseContents = angular.element('<div></div>').html(response).contents();
compileFn = cache[src] = $compile(responseContents);
compileFn(scope, cloneAttachFn);
});
}
};
};
var ngIncludeCached = function ($compileCache) {
return {
restrict: 'ECA',
terminal: true,
compile: function (element, attr) {
var srcExp = attr.ngIncludeCached || attr.src;
return function (scope, element) {
var src = scope.$eval(srcExp);
var newScope = scope.$new();
$compileCache(src, newScope, function (compiledElm, newScope) {
element.append(compiledElm);
});
};
}
};
};
angular.module("app")
.factory("$compileCache", ['$http', '$templateCache', '$compile', $compileCache])
.directive("ngIncludeCached", ['$compileCache', ngIncludeCached]);
@barnash
Copy link

barnash commented Jun 25, 2014

Why do you use the 'terminal' attribute on the directive?
AFAIK terminal: true would have no affect without priority.

@guillaume86
Copy link
Author

Very late I know: It was just a copy of ngInclude with some changes, must comes from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment