1
1
'use strict' ;
2
2
3
3
ngRouteModule . directive ( 'ngView' , ngViewFactory ) ;
4
+ ngRouteModule . directive ( 'ngView' , ngViewFillContentFactory ) ;
5
+
4
6
5
7
/**
6
8
* @ngdoc directive
@@ -166,8 +168,8 @@ ngRouteModule.directive('ngView', ngViewFactory);
166
168
* @description
167
169
* Emitted every time the ngView content is reloaded.
168
170
*/
169
- ngViewFactory . $inject = [ '$route' , '$anchorScroll' , '$compile' , '$controller' , '$ animate'] ;
170
- function ngViewFactory ( $route , $anchorScroll , $compile , $controller , $ animate) {
171
+ ngViewFactory . $inject = [ '$route' , '$anchorScroll' , '$animate' ] ;
172
+ function ngViewFactory ( $route , $anchorScroll , $animate ) {
171
173
return {
172
174
restrict : 'ECA' ,
173
175
terminal : true ,
@@ -199,41 +201,26 @@ function ngViewFactory( $route, $anchorScroll, $compile, $controller,
199
201
200
202
if ( template ) {
201
203
var newScope = scope . $new ( ) ;
204
+ var current = $route . current ;
202
205
203
206
// Note: This will also link all children of ng-view that were contained in the original
204
207
// html. If that content contains controllers, ... they could pollute/change the scope.
205
208
// However, using ng-view on an element with additional content does not make sense...
206
209
// Note: We can't remove them in the cloneAttchFn of $transclude as that
207
210
// function is called before linking the content, which would apply child
208
211
// directives to non existing elements.
209
- var clone = $transclude ( newScope , angular . noop ) ;
210
- clone . html ( template ) ;
211
- $animate . enter ( clone , null , currentElement || $element , function onNgViewEnter ( ) {
212
- if ( angular . isDefined ( autoScrollExp )
213
- && ( ! autoScrollExp || scope . $eval ( autoScrollExp ) ) ) {
214
- $anchorScroll ( ) ;
215
- }
212
+ var clone = $transclude ( newScope , function ( clone ) {
213
+ $animate . enter ( clone , null , currentElement || $element , function onNgViewEnter ( ) {
214
+ if ( angular . isDefined ( autoScrollExp )
215
+ && ( ! autoScrollExp || scope . $eval ( autoScrollExp ) ) ) {
216
+ $anchorScroll ( ) ;
217
+ }
218
+ } ) ;
219
+ cleanupLastView ( ) ;
216
220
} ) ;
217
221
218
- cleanupLastView ( ) ;
219
-
220
- var link = $compile ( clone . contents ( ) ) ,
221
- current = $route . current ;
222
-
223
- currentScope = current . scope = newScope ;
224
222
currentElement = clone ;
225
-
226
- if ( current . controller ) {
227
- locals . $scope = currentScope ;
228
- var controller = $controller ( current . controller , locals ) ;
229
- if ( current . controllerAs ) {
230
- currentScope [ current . controllerAs ] = controller ;
231
- }
232
- clone . data ( '$ngControllerController' , controller ) ;
233
- clone . children ( ) . data ( '$ngControllerController' , controller ) ;
234
- }
235
-
236
- link ( currentScope ) ;
223
+ currentScope = current . scope = newScope ;
237
224
currentScope . $emit ( '$viewContentLoaded' ) ;
238
225
currentScope . $eval ( onloadExp ) ;
239
226
} else {
@@ -243,3 +230,36 @@ function ngViewFactory( $route, $anchorScroll, $compile, $controller,
243
230
}
244
231
} ;
245
232
}
233
+
234
+ // This directive is called during the $transclude call of the first `ngView` directive.
235
+ // It will replace and compile the content of the element with the loaded template.
236
+ // We need this directive so that the element content is already filled when
237
+ // the link function of another directive on the same element as ngView
238
+ // is called.
239
+ ngViewFillContentFactory . $inject = [ '$compile' , '$controller' , '$route' ] ;
240
+ function ngViewFillContentFactory ( $compile , $controller , $route ) {
241
+ return {
242
+ restrict : 'ECA' ,
243
+ priority : - 400 ,
244
+ link : function ( scope , $element ) {
245
+ var current = $route . current ,
246
+ locals = current . locals ;
247
+
248
+ $element . html ( locals . $template ) ;
249
+
250
+ var link = $compile ( $element . contents ( ) ) ;
251
+
252
+ if ( current . controller ) {
253
+ locals . $scope = scope ;
254
+ var controller = $controller ( current . controller , locals ) ;
255
+ if ( current . controllerAs ) {
256
+ scope [ current . controllerAs ] = controller ;
257
+ }
258
+ $element . data ( '$ngControllerController' , controller ) ;
259
+ $element . children ( ) . data ( '$ngControllerController' , controller ) ;
260
+ }
261
+
262
+ link ( scope ) ;
263
+ }
264
+ } ;
265
+ }
0 commit comments