1
1
'use strict' ;
2
2
3
+ ngRouteModule . directive ( 'ngView' , ngViewFactory ) ;
4
+
3
5
/**
4
6
* @ngdoc directive
5
7
* @name ngRoute.directive:ngView
167
169
* @description
168
170
* Emitted every time the ngView content is reloaded.
169
171
*/
170
- var NG_VIEW_PRIORITY = 500 ;
171
- var ngViewDirective = [ '$route' , '$anchorScroll' , '$compile' , '$controller' , '$animate' ,
172
- function ( $route , $anchorScroll , $compile , $controller , $animate ) {
172
+ ngViewFactory . $inject = [ '$route' , '$anchorScroll' , '$compile' , '$controller' , '$animate' ] ;
173
+ function ngViewFactory ( $route , $anchorScroll , $compile , $controller , $animate ) {
173
174
return {
174
175
restrict : 'ECA' ,
175
176
terminal : true ,
176
- priority : NG_VIEW_PRIORITY ,
177
- compile : function ( element , attr ) {
178
- var onloadExp = attr . onload || '' ;
179
-
180
- element . html ( '' ) ;
181
- var anchor = jqLite ( document . createComment ( ' ngView ' ) ) ;
182
- element . replaceWith ( anchor ) ;
183
-
184
- return function ( scope ) {
185
- var currentScope , currentElement ;
177
+ transclude : 'element' ,
178
+ compile : function ( element , attr , linker ) {
179
+ return function ( scope , $element , attr ) {
180
+ var currentScope ,
181
+ currentElement ,
182
+ onloadExp = attr . onload || '' ;
186
183
187
184
scope . $on ( '$routeChangeSuccess' , update ) ;
188
185
update ( ) ;
@@ -203,42 +200,41 @@ var ngViewDirective = ['$route', '$anchorScroll', '$compile', '$controller', '$a
203
200
template = locals && locals . $template ;
204
201
205
202
if ( template ) {
206
- cleanupLastView ( ) ;
207
-
208
- currentScope = scope . $new ( ) ;
209
- currentElement = element . clone ( ) ;
210
- currentElement . html ( template ) ;
211
- $animate . enter ( currentElement , null , anchor ) ;
212
-
213
- var link = $compile ( currentElement , false , NG_VIEW_PRIORITY - 1 ) ,
214
- current = $route . current ;
215
-
216
- if ( current . controller ) {
217
- locals . $scope = currentScope ;
218
- var controller = $controller ( current . controller , locals ) ;
219
- if ( current . controllerAs ) {
220
- currentScope [ current . controllerAs ] = controller ;
203
+ var newScope = scope . $new ( ) ;
204
+ linker ( newScope , function ( clone ) {
205
+ cleanupLastView ( ) ;
206
+
207
+ clone . html ( template ) ;
208
+ $animate . enter ( clone , null , $element ) ;
209
+
210
+ var link = $compile ( clone . contents ( ) ) ,
211
+ current = $route . current ;
212
+
213
+ currentScope = current . scope = newScope ;
214
+ currentElement = clone ;
215
+
216
+ if ( current . controller ) {
217
+ locals . $scope = currentScope ;
218
+ var controller = $controller ( current . controller , locals ) ;
219
+ if ( current . controllerAs ) {
220
+ currentScope [ current . controllerAs ] = controller ;
221
+ }
222
+ clone . data ( '$ngControllerController' , controller ) ;
223
+ clone . contents ( ) . data ( '$ngControllerController' , controller ) ;
221
224
}
222
- currentElement . data ( '$ngControllerController' , controller ) ;
223
- currentElement . children ( ) . data ( '$ngControllerController' , controller ) ;
224
- }
225
-
226
- current . scope = currentScope ;
227
225
228
- link ( currentScope ) ;
226
+ link ( currentScope ) ;
227
+ currentScope . $emit ( '$viewContentLoaded' ) ;
228
+ currentScope . $eval ( onloadExp ) ;
229
229
230
- currentScope . $emit ( '$viewContentLoaded' ) ;
231
- currentScope . $eval ( onloadExp ) ;
232
-
233
- // $anchorScroll might listen on event...
234
- $anchorScroll ( ) ;
230
+ // $anchorScroll might listen on event...
231
+ $anchorScroll ( ) ;
232
+ } ) ;
235
233
} else {
236
234
cleanupLastView ( ) ;
237
235
}
238
236
}
239
237
}
240
238
}
241
239
} ;
242
- } ] ;
243
-
244
- ngRouteModule . directive ( 'ngView' , ngViewDirective ) ;
240
+ }
0 commit comments