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

Commit

Permalink
fix(ngController): fix issue with ngInclude on the same element
Browse files Browse the repository at this point in the history
This changes the priority of ngController to 500 so that it takes precedence
over ngInclude.

Closes #4431, #4521
  • Loading branch information
asilluron authored and btford committed Nov 21, 2013
1 parent f6ecf9a commit 6288cf5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ng/directive/ngController.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
var ngControllerDirective = [function() {
return {
scope: true,
controller: '@'
controller: '@',
priority: 500
};
}];
55 changes: 55 additions & 0 deletions test/ng/directive/ngControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div><div ng-controller="GreeterController" ng-include="\'url\'"></div></div>')($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('<div><div ng-controller="CountController" ng-include="url"></div></div>')($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('<div><div ng-controller="ExposeScopeController" ng-include="\'url\'"></div></div>')($rootScope);
$httpBackend.expect('GET', 'url').respond('<div ng-init="name=\'Vojta\'"></div>');
$rootScope.$digest();
$httpBackend.flush();
expect(controllerScope.name).toBeUndefined();
}));
});

0 comments on commit 6288cf5

Please sign in to comment.