147147 * @description
148148 * Emitted every time the ngInclude content is reloaded.
149149 */
150- var ngIncludeDirective = [ '$http' , '$templateCache' , '$anchorScroll' , '$compile' , '$ animate', '$sce' ,
151- function ( $http , $templateCache , $anchorScroll , $compile , $ animate, $sce ) {
150+ var ngIncludeDirective = [ '$http' , '$templateCache' , '$anchorScroll' , '$animate' , '$sce' ,
151+ function ( $http , $templateCache , $anchorScroll , $animate , $sce ) {
152152 return {
153153 restrict : 'ECA' ,
154154 priority : 400 ,
155155 terminal : true ,
156156 transclude : 'element' ,
157+ controller : angular . noop ,
157158 compile : function ( element , attr ) {
158159 var srcExp = attr . ngInclude || attr . src ,
159160 onloadExp = attr . onload || '' ,
@@ -187,22 +188,22 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
187188 $http . get ( src , { cache : $templateCache } ) . success ( function ( response ) {
188189 if ( thisChangeId !== changeCounter ) return ;
189190 var newScope = scope . $new ( ) ;
191+ ctrl . template = response ;
190192
191193 // Note: This will also link all children of ng-include that were contained in the original
192194 // html. If that content contains controllers, ... they could pollute/change the scope.
193195 // However, using ng-include on an element with additional content does not make sense...
194196 // Note: We can't remove them in the cloneAttchFn of $transclude as that
195197 // function is called before linking the content, which would apply child
196198 // directives to non existing elements.
197- var clone = $transclude ( newScope , noop ) ;
198- cleanupLastIncludeContent ( ) ;
199+ var clone = $transclude ( newScope , function ( clone ) {
200+ cleanupLastIncludeContent ( ) ;
201+ $animate . enter ( clone , null , $element , afterAnimation ) ;
202+ } ) ;
199203
200204 currentScope = newScope ;
201205 currentElement = clone ;
202206
203- currentElement . html ( response ) ;
204- $animate . enter ( currentElement , null , $element , afterAnimation ) ;
205- $compile ( currentElement . contents ( ) ) ( currentScope ) ;
206207 currentScope . $emit ( '$includeContentLoaded' ) ;
207208 scope . $eval ( onloadExp ) ;
208209 } ) . error ( function ( ) {
@@ -211,9 +212,28 @@ var ngIncludeDirective = ['$http', '$templateCache', '$anchorScroll', '$compile'
211212 scope . $emit ( '$includeContentRequested' ) ;
212213 } else {
213214 cleanupLastIncludeContent ( ) ;
215+ ctrl . template = null ;
214216 }
215217 } ) ;
216218 } ;
217219 }
218220 } ;
219221} ] ;
222+
223+ // This directive is called during the $transclude call of the first `ngInclude` directive.
224+ // It will replace and compile the content of the element with the loaded template.
225+ // We need this directive so that the element content is already filled when
226+ // the link function of another directive on the same element as ngInclude
227+ // is called.
228+ var ngIncludeFillContentDirective = [ '$compile' ,
229+ function ( $compile ) {
230+ return {
231+ restrict : 'ECA' ,
232+ priority : - 400 ,
233+ require : 'ngInclude' ,
234+ link : function ( scope , $element , $attr , ctrl ) {
235+ $element . html ( ctrl . template ) ;
236+ $compile ( $element . contents ( ) ) ( scope ) ;
237+ }
238+ } ;
239+ } ] ;
0 commit comments