aboutsummaryrefslogtreecommitdiffstats
path: root/src/ng/directive/ngView.js
diff options
context:
space:
mode:
authorVojta Jina2012-03-30 15:03:20 -0700
committerVojta Jina2012-04-03 10:10:44 -0700
commit15c1fe392942b70e456f10afbdfd9c3329249a43 (patch)
treebec93d124ab8bc1cbd283e250be5b037bcc1fce7 /src/ng/directive/ngView.js
parent428f2b563663315df4f235ca19cef4bdcf82e2ab (diff)
downloadangular.js-15c1fe392942b70e456f10afbdfd9c3329249a43.tar.bz2
refactor(ngView): remove extra $watch, refactor one ugly test
Diffstat (limited to 'src/ng/directive/ngView.js')
-rw-r--r--src/ng/directive/ngView.js36
1 files changed, 19 insertions, 17 deletions
diff --git a/src/ng/directive/ngView.js b/src/ng/directive/ngView.js
index 95b1546d..b305af0b 100644
--- a/src/ng/directive/ngView.js
+++ b/src/ng/directive/ngView.js
@@ -115,42 +115,44 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
lastScope,
onloadExp = attr.onload || '';
- scope.$on('$afterRouteChange', function(event, next, previous) {
- changeCounter++;
- });
+ scope.$on('$afterRouteChange', update);
+ update();
- scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
- var template = $route.current && $route.current.template;
- function destroyLastScope() {
- if (lastScope) {
- lastScope.$destroy();
- lastScope = null;
- }
+ function destroyLastScope() {
+ if (lastScope) {
+ lastScope.$destroy();
+ lastScope = null;
}
+ }
+
+ function update() {
+ var template = $route.current && $route.current.template,
+ thisChangeId = ++changeCounter;
function clearContent() {
// ignore callback if another route change occured since
- if (newChangeCounter == changeCounter) {
+ if (thisChangeId === changeCounter) {
element.html('');
+ destroyLastScope();
}
- destroyLastScope();
}
if (template) {
$http.get(template, {cache: $templateCache}).success(function(response) {
// ignore callback if another route change occured since
- if (newChangeCounter == changeCounter) {
+ if (thisChangeId === changeCounter) {
element.html(response);
destroyLastScope();
var link = $compile(element.contents()),
- current = $route.current;
+ current = $route.current,
+ controller;
lastScope = current.scope = scope.$new();
if (current.controller) {
- element.contents().
- data('$ngControllerController', $controller(current.controller, {$scope: lastScope}));
+ controller = $controller(current.controller, {$scope: lastScope});
+ element.contents().data('$ngControllerController', controller);
}
link(lastScope);
@@ -164,7 +166,7 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
} else {
clearContent();
}
- });
+ }
}
};
}];