Skip to content

Instantly share code, notes, and snippets.

@Thorsson
Created November 26, 2014 16:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thorsson/738c46285828ed95fd79 to your computer and use it in GitHub Desktop.
Save Thorsson/738c46285828ed95fd79 to your computer and use it in GitHub Desktop.
(function(){
'use strict';
angular
.module('app', [])
.directive('ngInsert', ngInsert)
.directive('ngInsert', ngInsertFillContentDirective);
ngInsert = ['$templateRequest', '$anchorScroll', '$animate', '$sce'];
function ngInsert($templateRequest, $anchorScroll, $animate, $sce) {
return {
restrict: 'ECA',
priority: 400,
terminal: true,
transclude: 'element',
controller: angular.noop,
compile: function(element, attr) {
var srcExp = attr.ngInsert || attr.src,
onloadExp = attr.onload || '',
preserveScope = attr.preserveScope || true,
autoScrollExp = attr.autoscroll;
return function(scope, $element, $attr, ctrl, $transclude) {
var changeCounter = 0,
currentScope,
previousElement,
currentElement;
var cleanupLastInsertContent = function() {
if (previousElement) {
previousElement.remove();
previousElement = null;
}
if (currentScope) {
currentScope.$destroy();
currentScope = null;
}
if (currentElement) {
$animate.leave(currentElement).then(function() {
previousElement = null;
});
previousElement = currentElement;
currentElement = null;
}
};
scope.$watch($sce.parseAsResourceUrl(srcExp), function ngIncludeWatchAction(src) {
var afterAnimation = function() {
if (angular.isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
$anchorScroll();
}
};
var thisChangeId = ++changeCounter;
if (src) {
$templateRequest(src, true).then(function(response) {
if (thisChangeId !== changeCounter) return;
var newScope = scope.$parent;
if (!preserveScope)
newScope = scope.$new();
ctrl.template = response;
var clone = $transclude(newScope, function(clone) {
cleanupLastInsertContent();
$animate.enter(clone, null, $element).then(afterAnimation);
});
currentScope = newScope;
currentElement = clone;
currentScope.$emit('$insertContentLoaded', src);
scope.$eval(onloadExp);
}, function() {
if (thisChangeId === changeCounter) {
cleanupLastInsertContent();
scope.$emit('$insertContentError', src);
}
});
scope.$emit('$insertContentRequested', src);
} else {
cleanupLastInsertContent();
ctrl.template = null;
}
});
};
}
};
}
ngInsertFillContentDirective = ['$compile'];
function ngInsertFillContentDirective($compile) {
return {
restrict: 'ECA',
priority: -400,
require: 'ngInsert',
link: function(scope, $element, $attr, ctrl) {
if (/SVG/.test($element[0].toString())) {
// WebKit: https://bugs.webkit.org/show_bug.cgi?id=135698 --- SVG elements do not
// support innerHTML, so detect this here and try to generate the contents
// specially.
$element.empty();
$compile(jqLiteBuildFragment(ctrl.template, document).childNodes)(scope,
function namespaceAdaptedClone(clone) {
$element.append(clone);
}, {futureParentElement: $element});
return;
}
$element.html(ctrl.template);
$compile($element.contents())(scope);
}
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment