Last active
December 21, 2015 02:59
-
-
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".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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(); | |
}); | |
}; | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!