Skip to content

Commit bc6aec6

Browse files
caitpjamesdaily
authored andcommitted
fix(ngRoute): instantiate controller when template is empty
Before this change, $route controllers are not instantiated if the template is falsy, which includes the empty string. This change tests if the template is not undefined, rather than just falsy, in order to ensure that templates are instantiated even when the template is empty, which people may have some reason to do. This "bug" was reported in http://robb.weblaws.org/2013/06/21/angularjs-vs-emberjs/, as a "gotcha" for AngularJS / ngRoute. Closes angular#5550
1 parent 55befc1 commit bc6aec6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/ngRoute/directive/ngView.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function ngViewFactory( $route, $anchorScroll, $animate) {
199199
var locals = $route.current && $route.current.locals,
200200
template = locals && locals.$template;
201201

202-
if (template) {
202+
if (angular.isDefined(template)) {
203203
var newScope = scope.$new();
204204
var current = $route.current;
205205

test/ngRoute/directive/ngViewSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ describe('ngView', function() {
5656
});
5757

5858

59+
it('should instantiate controller for empty template', function() {
60+
var log = [], controllerScope,
61+
Ctrl = function($scope) {
62+
controllerScope = $scope;
63+
log.push('ctrl-init');
64+
};
65+
66+
module(function($routeProvider) {
67+
$routeProvider.when('/some', {templateUrl: '/tpl.html', controller: Ctrl});
68+
});
69+
70+
inject(function($route, $rootScope, $templateCache, $location) {
71+
$templateCache.put('/tpl.html', [200, '', {}]);
72+
$location.path('/some');
73+
$rootScope.$digest();
74+
75+
expect(controllerScope.$parent).toBe($rootScope);
76+
expect(controllerScope).toBe($route.current.scope);
77+
expect(log).toEqual(['ctrl-init']);
78+
});
79+
});
80+
81+
5982
it('should instantiate controller with an alias', function() {
6083
var log = [], controllerScope,
6184
Ctrl = function($scope) {

0 commit comments

Comments
 (0)