Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit dc954f2

Browse files
fix(): allow to be included in an asynchronously loaded template
1 parent c8a6481 commit dc954f2

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

src/ngRoute/route.js

+42-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,40 @@ var isObject;
2323
* <div doc-module-components="ngRoute"></div>
2424
*/
2525
/* global -ngRouteModule */
26-
var ngRouteModule = angular.module('ngRoute', ['ng']).
27-
provider('$route', $RouteProvider),
26+
var ngRouteModule = angular.module('ngRoute', ['ng'])
27+
.provider('$route', $RouteProvider)
28+
.factory('$$trackLocationChanges', ['$rootScope', $$trackLocationChanges])
29+
.run(['$$trackLocationChanges', function($$trackLocationChanges) {
30+
$$trackLocationChanges.start();
31+
}]),
32+
2833
$routeMinErr = angular.$$minErr('ngRoute');
2934

35+
function $$trackLocationChanges($rootScope) {
36+
var removeStartHandler, removeSuccessHandler;
37+
var service = {
38+
start: function() {
39+
removeStartHandler = $rootScope.$on('$locationChangeStart', storeStartEvent);
40+
removeSuccessHandler = $rootScope.$on('$locationChangeSuccess', storeSuccessEvent);
41+
},
42+
stop: function() {
43+
removeStartHandler();
44+
removeSuccessHandler();
45+
}
46+
};
47+
48+
return service;
49+
50+
function storeStartEvent(e) {
51+
service.startEvent = e;
52+
delete service.successEvent;
53+
}
54+
55+
function storeSuccessEvent(e) {
56+
service.successEvent = e;
57+
}
58+
}
59+
3060
/**
3161
* @ngdoc provider
3262
* @name $routeProvider
@@ -295,7 +325,9 @@ function $RouteProvider() {
295325
'$injector',
296326
'$templateRequest',
297327
'$sce',
298-
function($rootScope, $location, $routeParams, $q, $injector, $templateRequest, $sce) {
328+
'$$trackLocationChanges',
329+
function($rootScope, $location, $routeParams, $q, $injector, $templateRequest, $sce, $$trackLocationChanges) {
330+
299331

300332
/**
301333
* @ngdoc service
@@ -555,6 +587,12 @@ function $RouteProvider() {
555587
}
556588
};
557589

590+
if ($$trackLocationChanges.successEvent) {
591+
prepareRoute($$trackLocationChanges.startEvent);
592+
commitRoute($$trackLocationChanges.successEvent);
593+
}
594+
$$trackLocationChanges.stop();
595+
558596
$rootScope.$on('$locationChangeStart', prepareRoute);
559597
$rootScope.$on('$locationChangeSuccess', commitRoute);
560598

@@ -612,6 +650,7 @@ function $RouteProvider() {
612650
}
613651

614652
function commitRoute() {
653+
$route.initialized = true;
615654
var lastRoute = $route.current;
616655
var nextRoute = preparedRoute;
617656

test/ngRoute/directive/ngViewSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,37 @@ describe('ngView', function() {
695695
});
696696
});
697697

698+
describe('ngView in async template', function() {
699+
beforeEach(module('ngRoute'));
700+
beforeEach(module(function($compileProvider, $provide, $routeProvider) {
701+
$compileProvider.directive('asyncView', function() {
702+
return {templateUrl: 'async-view.html'};
703+
});
704+
705+
$provide.decorator('$templateRequest', function($timeout) {
706+
return function() {
707+
return $timeout(angular.identity, 500, false, '<ng-view></ng-view>');
708+
};
709+
});
710+
711+
$routeProvider.when('/', {template: 'Hello, world!'});
712+
}));
713+
714+
715+
it('should work correctly upon initial page load',
716+
// Injecting `$location` here is necessary, so that it gets instantiated early
717+
inject(function($compile, $location, $rootScope, $timeout) {
718+
var elem = $compile('<async-view></async-view>')($rootScope);
719+
$rootScope.$digest();
720+
$timeout.flush(500);
721+
722+
expect(elem.text()).toBe('Hello, world!');
723+
724+
dealoc(elem);
725+
})
726+
);
727+
});
728+
698729
describe('animations', function() {
699730
var body, element, $rootElement;
700731

0 commit comments

Comments
 (0)