Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
http-interceptor to add the location of a directives template.
app.config(function($provide, $httpProvider) {
labelDirectiveTemplates();
})
function labelDirectiveTemplates() {
$provide.decorator('$templateCache', function($delegate) {
var get = $delegate.get;
$delegate.get = decorateTemplateWithComment;
return $delegate;
function decorateTemplateWithComment(key) {
var value = get.call($delegate, key);
console.log('template::', key, value);
return addComment(key, value);
}
});
$httpProvider.interceptors.push(function($q) {
return {
'response': function(response) {
var url = response.config.url;
var isTemplate = url.indexOf('.html') > -1;
if (isTemplate) {
response.data = addComment(url, response.data);
}
return $q.when(response);
}
};
});
function addComment(url, template) {
if (!template)
return undefined;
if (angular.isArray(template))
template = template[1];
var commentBegin = '<!-- TEMPLATE-LOCATION: "';
var commentEnd = '" -->\n';
var comment = commentBegin + url + commentEnd;
if (template.indexOf(commentBegin) == -1)
return comment + template;
return template;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment