From 6288cf5ca471b0615a026fdb4db3ba242c9d8f88 Mon Sep 17 00:00:00 2001 From: Andrew Silluron-Gonzalez Date: Wed, 16 Oct 2013 11:35:42 -0700 Subject: [PATCH] fix(ngController): fix issue with ngInclude on the same element This changes the priority of ngController to 500 so that it takes precedence over ngInclude. Closes #4431, #4521 --- src/ng/directive/ngController.js | 3 +- test/ng/directive/ngControllerSpec.js | 55 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/ngController.js b/src/ng/directive/ngController.js index 6a7b8f034d99..5550b77fd852 100644 --- a/src/ng/directive/ngController.js +++ b/src/ng/directive/ngController.js @@ -167,6 +167,7 @@ var ngControllerDirective = [function() { return { scope: true, - controller: '@' + controller: '@', + priority: 500 }; }]; diff --git a/test/ng/directive/ngControllerSpec.js b/test/ng/directive/ngControllerSpec.js index 402ddf0901b1..9f6d343e6f2d 100644 --- a/test/ng/directive/ngControllerSpec.js +++ b/test/ng/directive/ngControllerSpec.js @@ -85,4 +85,59 @@ describe('ngController', function() { $rootScope.$digest(); expect(element.text()).toBe('Vojta'); })); + + + it('should work with ngInclude on the same element', inject(function($compile, $rootScope, $httpBackend) { + $rootScope.GreeterController = function($scope) { + $scope.name = 'Vojta'; + }; + + element = $compile('
')($rootScope); + $httpBackend.expect('GET', 'url').respond('{{name}}'); + $rootScope.$digest(); + $httpBackend.flush(); + expect(element.text()).toEqual('Vojta'); + })); + + + it('should only instantiate the controller once with ngInclude on the same element', + inject(function($compile, $rootScope, $httpBackend) { + + var count = 0; + + $rootScope.CountController = function($scope) { + count += 1; + }; + + element = $compile('
')($rootScope); + + $httpBackend.expect('GET', 'first').respond('first'); + $rootScope.url = 'first'; + $rootScope.$digest(); + $httpBackend.flush(); + + $httpBackend.expect('GET', 'second').respond('second'); + $rootScope.url = 'second'; + $rootScope.$digest(); + $httpBackend.flush(); + + expect(count).toBe(1); + })); + + + it('when ngInclude is on the same element, the content included content should get a child scope of the controller', + inject(function($compile, $rootScope, $httpBackend) { + + var controllerScope; + + $rootScope.ExposeScopeController = function($scope) { + controllerScope = $scope; + }; + + element = $compile('
')($rootScope); + $httpBackend.expect('GET', 'url').respond('
'); + $rootScope.$digest(); + $httpBackend.flush(); + expect(controllerScope.name).toBeUndefined(); + })); });