Skip to content

Instantly share code, notes, and snippets.

@cdmckay
Last active December 21, 2015 02:59
Show Gist options
  • Save cdmckay/6238612 to your computer and use it in GitHub Desktop.
Save cdmckay/6238612 to your computer and use it in GitHub Desktop.
The working 1.0.7 Angular.js ngInclude that works with Angular.js 1.2.0rc1 (which has a broken ngInclude). Just change "angularApp" to your app's name and use "cm-include" where you would use "ng-include".
'use strict';
angular.module('angularApp')
.directive('cmInclude', ['$http', '$templateCache', '$anchorScroll', '$compile',
function($http, $templateCache, $anchorScroll, $compile) {
return {
restrict: 'ECA',
terminal: true,
compile: function(element, attr) {
var srcExp = attr.ngInclude || attr.src,
onloadExp = attr.onload || '',
autoScrollExp = attr.autoscroll;
return function(scope, element) {
var changeCounter = 0,
childScope;
var clearContent = function() {
if (childScope) {
childScope.$destroy();
childScope = null;
}
element.html('');
};
scope.$watch(srcExp, function ngIncludeWatchAction(src) {
var thisChangeId = ++changeCounter;
if (src) {
$http.get(src, {cache: $templateCache}).success(function(response) {
if (thisChangeId !== changeCounter) return;
if (childScope) childScope.$destroy();
childScope = scope.$new();
element.html(response);
$compile(element.contents())(childScope);
if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {
$anchorScroll();
}
childScope.$emit('$includeContentLoaded');
scope.$eval(onloadExp);
}).error(function() {
if (thisChangeId === changeCounter) clearContent();
});
} else clearContent();
});
};
}
};
}]);
@matb33
Copy link

matb33 commented Aug 15, 2013

Line 10 should be: var srcExp = attr.cmInclude || attr.src,

Line 40 should be: if (angular.isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) {

Nonetheless, thank you for the temporary fix!

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