diff options
| author | Tobias Bosch | 2013-12-10 16:46:29 -0800 | 
|---|---|---|
| committer | Tobias Bosch | 2013-12-12 17:18:44 -0800 | 
| commit | 30a8b7d0b5d4882c2bf3b20eb696a02f5b667726 (patch) | |
| tree | 0c40067e5142a8a1dc71df8b03b39fb3e05b2878 /src/ng/directive/ngInclude.js | |
| parent | f8944efe70b81e02704df9b53ea2546c80c73d3b (diff) | |
| download | angular.js-30a8b7d0b5d4882c2bf3b20eb696a02f5b667726.tar.bz2 | |
fix(ngInclude): Add template to DOM before linking other directives
The template needs to be added to the DOM before
other directives at the same element as `ngInclude` are linked.
Fixes #5247.
Diffstat (limited to 'src/ng/directive/ngInclude.js')
| -rw-r--r-- | src/ng/directive/ngInclude.js | 34 | 
1 files changed, 27 insertions, 7 deletions
| diff --git a/src/ng/directive/ngInclude.js b/src/ng/directive/ngInclude.js index b721aa23..d6b38663 100644 --- a/src/ng/directive/ngInclude.js +++ b/src/ng/directive/ngInclude.js @@ -147,13 +147,14 @@   * @description   * Emitted every time the ngInclude content is reloaded.   */ -var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile', '$animate', '$sce', -                  function($http,   $templateCache,   $anchorScroll,   $compile,   $animate,   $sce) { +var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$animate', '$sce', +                  function($http,   $templateCache,   $anchorScroll,   $animate,   $sce) {    return {      restrict: 'ECA',      priority: 400,      terminal: true,      transclude: 'element', +    controller: angular.noop,      compile: function(element, attr) {        var srcExp = attr.ngInclude || attr.src,            onloadExp = attr.onload || '', @@ -187,6 +188,7 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'              $http.get(src, {cache: $templateCache}).success(function(response) {                if (thisChangeId !== changeCounter) return;                var newScope = scope.$new(); +              ctrl.template = response;                // Note: This will also link all children of ng-include that were contained in the original                // html. If that content contains controllers, ... they could pollute/change the scope. @@ -194,15 +196,14 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'                // Note: We can't remove them in the cloneAttchFn of $transclude as that                // function is called before linking the content, which would apply child                // directives to non existing elements. -              var clone = $transclude(newScope, noop); -              cleanupLastIncludeContent(); +              var clone = $transclude(newScope, function(clone) { +                cleanupLastIncludeContent(); +                $animate.enter(clone, null, $element, afterAnimation); +              });                currentScope = newScope;                currentElement = clone; -              currentElement.html(response); -              $animate.enter(currentElement, null, $element, afterAnimation); -              $compile(currentElement.contents())(currentScope);                currentScope.$emit('$includeContentLoaded');                scope.$eval(onloadExp);              }).error(function() { @@ -211,9 +212,28 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'              scope.$emit('$includeContentRequested');            } else {              cleanupLastIncludeContent(); +            ctrl.template = null;            }          });        };      }    };  }]; + +// This directive is called during the $transclude call of the first `ngInclude` directive. +// It will replace and compile the content of the element with the loaded template. +// We need this directive so that the element content is already filled when +// the link function of another directive on the same element as ngInclude +// is called. +var ngIncludeFillContentDirective = ['$compile', +  function($compile) { +    return { +      restrict: 'ECA', +      priority: -400, +      require: 'ngInclude', +      link: function(scope, $element, $attr, ctrl) { +        $element.html(ctrl.template); +        $compile($element.contents())(scope); +      } +    }; +  }]; | 
