@@ -115,42 +115,44 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
115
115
lastScope ,
116
116
onloadExp = attr . onload || '' ;
117
117
118
- scope . $on ( '$afterRouteChange' , function ( event , next , previous ) {
119
- changeCounter ++ ;
120
- } ) ;
118
+ scope . $on ( '$afterRouteChange' , update ) ;
119
+ update ( ) ;
121
120
122
- scope . $watch ( function ( ) { return changeCounter ; } , function ( newChangeCounter ) {
123
- var template = $route . current && $route . current . template ;
124
121
125
- function destroyLastScope ( ) {
126
- if ( lastScope ) {
127
- lastScope . $destroy ( ) ;
128
- lastScope = null ;
129
- }
122
+ function destroyLastScope ( ) {
123
+ if ( lastScope ) {
124
+ lastScope . $destroy ( ) ;
125
+ lastScope = null ;
130
126
}
127
+ }
128
+
129
+ function update ( ) {
130
+ var template = $route . current && $route . current . template ,
131
+ thisChangeId = ++ changeCounter ;
131
132
132
133
function clearContent ( ) {
133
134
// ignore callback if another route change occured since
134
- if ( newChangeCounter == changeCounter ) {
135
+ if ( thisChangeId = == changeCounter ) {
135
136
element . html ( '' ) ;
137
+ destroyLastScope ( ) ;
136
138
}
137
- destroyLastScope ( ) ;
138
139
}
139
140
140
141
if ( template ) {
141
142
$http . get ( template , { cache : $templateCache } ) . success ( function ( response ) {
142
143
// ignore callback if another route change occured since
143
- if ( newChangeCounter == changeCounter ) {
144
+ if ( thisChangeId = == changeCounter ) {
144
145
element . html ( response ) ;
145
146
destroyLastScope ( ) ;
146
147
147
148
var link = $compile ( element . contents ( ) ) ,
148
- current = $route . current ;
149
+ current = $route . current ,
150
+ controller ;
149
151
150
152
lastScope = current . scope = scope . $new ( ) ;
151
153
if ( current . controller ) {
152
- element . contents ( ) .
153
- data ( '$ngControllerController' , $ controller( current . controller , { $scope : lastScope } ) ) ;
154
+ controller = $controller ( current . controller , { $scope : lastScope } ) ;
155
+ element . contents ( ) . data ( '$ngControllerController' , controller ) ;
154
156
}
155
157
156
158
link ( lastScope ) ;
@@ -164,7 +166,7 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
164
166
} else {
165
167
clearContent ( ) ;
166
168
}
167
- } ) ;
169
+ }
168
170
}
169
171
} ;
170
172
} ] ;
0 commit comments