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

Commit 6288cf5

Browse files
asilluronbtford
authored andcommitted
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
1 parent f6ecf9a commit 6288cf5

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/ng/directive/ngController.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
var ngControllerDirective = [function() {
168168
return {
169169
scope: true,
170-
controller: '@'
170+
controller: '@',
171+
priority: 500
171172
};
172173
}];

test/ng/directive/ngControllerSpec.js

+55
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,59 @@ describe('ngController', function() {
8585
$rootScope.$digest();
8686
expect(element.text()).toBe('Vojta');
8787
}));
88+
89+
90+
it('should work with ngInclude on the same element', inject(function($compile, $rootScope, $httpBackend) {
91+
$rootScope.GreeterController = function($scope) {
92+
$scope.name = 'Vojta';
93+
};
94+
95+
element = $compile('<div><div ng-controller="GreeterController" ng-include="\'url\'"></div></div>')($rootScope);
96+
$httpBackend.expect('GET', 'url').respond('{{name}}');
97+
$rootScope.$digest();
98+
$httpBackend.flush();
99+
expect(element.text()).toEqual('Vojta');
100+
}));
101+
102+
103+
it('should only instantiate the controller once with ngInclude on the same element',
104+
inject(function($compile, $rootScope, $httpBackend) {
105+
106+
var count = 0;
107+
108+
$rootScope.CountController = function($scope) {
109+
count += 1;
110+
};
111+
112+
element = $compile('<div><div ng-controller="CountController" ng-include="url"></div></div>')($rootScope);
113+
114+
$httpBackend.expect('GET', 'first').respond('first');
115+
$rootScope.url = 'first';
116+
$rootScope.$digest();
117+
$httpBackend.flush();
118+
119+
$httpBackend.expect('GET', 'second').respond('second');
120+
$rootScope.url = 'second';
121+
$rootScope.$digest();
122+
$httpBackend.flush();
123+
124+
expect(count).toBe(1);
125+
}));
126+
127+
128+
it('when ngInclude is on the same element, the content included content should get a child scope of the controller',
129+
inject(function($compile, $rootScope, $httpBackend) {
130+
131+
var controllerScope;
132+
133+
$rootScope.ExposeScopeController = function($scope) {
134+
controllerScope = $scope;
135+
};
136+
137+
element = $compile('<div><div ng-controller="ExposeScopeController" ng-include="\'url\'"></div></div>')($rootScope);
138+
$httpBackend.expect('GET', 'url').respond('<div ng-init="name=\'Vojta\'"></div>');
139+
$rootScope.$digest();
140+
$httpBackend.flush();
141+
expect(controllerScope.name).toBeUndefined();
142+
}));
88143
});

0 commit comments

Comments
 (0)