diff options
| author | Caitlin Potter | 2013-12-27 20:25:51 -0500 | 
|---|---|---|
| committer | Igor Minar | 2013-12-27 22:45:46 -0800 | 
| commit | 498365f219f65d6c29bdf2f03610a4d3646009bb (patch) | |
| tree | f0bd42b70c6c150a0cede291dc25d3e7a4ed3b06 | |
| parent | 056c8493521988dbb330c6636135b505737da918 (diff) | |
| download | angular.js-498365f219f65d6c29bdf2f03610a4d3646009bb.tar.bz2 | |
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 #5550
| -rw-r--r-- | src/ngRoute/directive/ngView.js | 2 | ||||
| -rw-r--r-- | test/ngRoute/directive/ngViewSpec.js | 23 | 
2 files changed, 24 insertions, 1 deletions
| diff --git a/src/ngRoute/directive/ngView.js b/src/ngRoute/directive/ngView.js index b1252944..0ae1c4fc 100644 --- a/src/ngRoute/directive/ngView.js +++ b/src/ngRoute/directive/ngView.js @@ -199,7 +199,7 @@ function ngViewFactory(   $route,   $anchorScroll,   $animate) {            var locals = $route.current && $route.current.locals,                template = locals && locals.$template; -          if (template) { +          if (angular.isDefined(template)) {              var newScope = scope.$new();              var current = $route.current; diff --git a/test/ngRoute/directive/ngViewSpec.js b/test/ngRoute/directive/ngViewSpec.js index a8d1f459..cb3455e6 100644 --- a/test/ngRoute/directive/ngViewSpec.js +++ b/test/ngRoute/directive/ngViewSpec.js @@ -56,6 +56,29 @@ describe('ngView', function() {    }); +  it('should instantiate controller for empty template', function() { +    var log = [], controllerScope, +        Ctrl = function($scope) { +          controllerScope = $scope; +          log.push('ctrl-init'); +        }; + +    module(function($routeProvider) { +      $routeProvider.when('/some', {templateUrl: '/tpl.html', controller: Ctrl}); +    }); + +    inject(function($route, $rootScope, $templateCache, $location) { +      $templateCache.put('/tpl.html', [200, '', {}]); +      $location.path('/some'); +      $rootScope.$digest(); + +      expect(controllerScope.$parent).toBe($rootScope); +      expect(controllerScope).toBe($route.current.scope); +      expect(log).toEqual(['ctrl-init']); +    }); +  }); + +    it('should instantiate controller with an alias', function() {      var log = [], controllerScope,          Ctrl = function($scope) { | 
