From c2989f6cc6ea8be34101b9ab9bc25d14a2468e20 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Sun, 4 Mar 2012 00:53:45 -0800 Subject: fix(ng-include): Compile only content --- src/widgets.js | 82 ++++++++++++++++++++++++++--------------------------- test/widgetsSpec.js | 18 ++++++++++++ 2 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/widgets.js b/src/widgets.js index a32abb75..66ea7fef 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -69,51 +69,49 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile' var srcExp = attr.src, scopeExp = attr.scope || '', autoScrollExp = attr.autoscroll; - if (!element[0]['ng:compiled']) { - element[0]['ng:compiled'] = true; - return function(scope, element, attr){ - var changeCounter = 0, - childScope; - - function incrementChange() { changeCounter++;} - scope.$watch(srcExp, incrementChange); - scope.$watch(function() { - var includeScope = scope.$eval(scopeExp); - if (includeScope) return includeScope.$id; - }, incrementChange); - scope.$watch(function() {return changeCounter;}, function(newChangeCounter) { - var src = scope.$eval(srcExp), - useScope = scope.$eval(scopeExp); - - function clearContent() { - // if this callback is still desired - if (newChangeCounter === changeCounter) { - if (childScope) childScope.$destroy(); - childScope = null; - element.html(''); - } + + return function(scope, element, attr) { + var changeCounter = 0, + childScope; + + function incrementChange() { changeCounter++;} + scope.$watch(srcExp, incrementChange); + scope.$watch(function() { + var includeScope = scope.$eval(scopeExp); + if (includeScope) return includeScope.$id; + }, incrementChange); + scope.$watch(function() {return changeCounter;}, function(newChangeCounter) { + var src = scope.$eval(srcExp), + useScope = scope.$eval(scopeExp); + + function clearContent() { + // if this callback is still desired + if (newChangeCounter === changeCounter) { + if (childScope) childScope.$destroy(); + childScope = null; + element.html(''); } + } - if (src) { - $http.get(src, {cache: $templateCache}).success(function(response) { - // if this callback is still desired - if (newChangeCounter === changeCounter) { - element.html(response); - if (childScope) childScope.$destroy(); - childScope = useScope ? useScope : scope.$new(); - $compile(element)(childScope); - if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) { - $anchorScroll(); - } - scope.$emit('$contentLoaded'); + if (src) { + $http.get(src, {cache: $templateCache}).success(function(response) { + // if this callback is still desired + if (newChangeCounter === changeCounter) { + element.html(response); + if (childScope) childScope.$destroy(); + childScope = useScope ? useScope : scope.$new(); + $compile(element.contents())(childScope); + if (isDefined(autoScrollExp) && (!autoScrollExp || scope.$eval(autoScrollExp))) { + $anchorScroll(); } - }).error(clearContent); - } else { - clearContent(); - } - }); - }; - } + scope.$emit('$contentLoaded'); + } + }).error(clearContent); + } else { + clearContent(); + } + }); + }; } } }]; diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index f75d81c2..3d2f0a56 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -245,6 +245,24 @@ describe('widget', function() { })); + it('should compile only the content', inject(function($compile, $rootScope, $templateCache) { + // regression + + var onload = jasmine.createSpy('$contentLoaded'); + $rootScope.$on('$contentLoaded', onload); + $templateCache.put('tpl.html', [200, 'partial {{tpl}}', {}]); + + element = $compile('