@@ -199,37 +199,43 @@ function ngViewFactory( $route, $anchorScroll, $compile, $controller,
199199
200200 if ( template ) {
201201 var newScope = scope . $new ( ) ;
202- $transclude ( newScope , function ( clone ) {
203- clone . html ( template ) ;
204- $animate . enter ( clone , null , currentElement || $element , function onNgViewEnter ( ) {
205- if ( angular . isDefined ( autoScrollExp )
206- && ( ! autoScrollExp || scope . $eval ( autoScrollExp ) ) ) {
207- $anchorScroll ( ) ;
208- }
209- } ) ;
210-
211- cleanupLastView ( ) ;
212-
213- var link = $compile ( clone . contents ( ) ) ,
214- current = $route . current ;
215-
216- currentScope = current . scope = newScope ;
217- currentElement = clone ;
218-
219- if ( current . controller ) {
220- locals . $scope = currentScope ;
221- var controller = $controller ( current . controller , locals ) ;
222- if ( current . controllerAs ) {
223- currentScope [ current . controllerAs ] = controller ;
224- }
225- clone . data ( '$ngControllerController' , controller ) ;
226- clone . children ( ) . data ( '$ngControllerController' , controller ) ;
227- }
228202
229- link ( currentScope ) ;
230- currentScope . $emit ( '$viewContentLoaded' ) ;
231- currentScope . $eval ( onloadExp ) ;
203+ // Note: This will also link all children of ng-view that were contained in the original
204+ // html. If that content contains controllers, ... they could pollute/change the scope.
205+ // However, using ng-view on an element with additional content does not make sense...
206+ // Note: We can't remove them in the cloneAttchFn of $transclude as that
207+ // function is called before linking the content, which would apply child
208+ // 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+ }
232216 } ) ;
217+
218+ cleanupLastView ( ) ;
219+
220+ var link = $compile ( clone . contents ( ) ) ,
221+ current = $route . current ;
222+
223+ currentScope = current . scope = newScope ;
224+ 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 ) ;
237+ currentScope . $emit ( '$viewContentLoaded' ) ;
238+ currentScope . $eval ( onloadExp ) ;
233239 } else {
234240 cleanupLastView ( ) ;
235241 }
0 commit comments