Skip to content

Commit aa7d45e

Browse files
authored
fix($compile): use correct parent element when requiring on html element
Fixes angular#16535 Closes angular#16647
1 parent 6b915ad commit aa7d45e

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/ng/compile.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
29602960

29612961
if (!value) {
29622962
var dataName = '$' + name + 'Controller';
2963-
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
2963+
2964+
if (inheritType === '^^' && $element[0] && $element[0].nodeType === NODE_TYPE_DOCUMENT) {
2965+
// inheritedData() uses the documentElement when it finds the document, so we would
2966+
// require from the element itself.
2967+
value = null;
2968+
} else {
2969+
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
2970+
}
29642971
}
29652972

29662973
if (!value && !optional) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html ng-app="test" require-directive require-target-directive>
3+
<body>
4+
<div id="container"></div>
5+
<script src="angular.js"></script>
6+
<script src="script.js"></script>
7+
</body>
8+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
angular.
4+
module('test', []).
5+
provider('$exceptionHandler', /** @this */ function() {
6+
this.$get = [function() {
7+
return function(error) {
8+
window.document.querySelector('#container').textContent = error && error.message;
9+
};
10+
}];
11+
}).
12+
13+
directive('requireDirective', function() {
14+
return {
15+
require: '^^requireTargetDirective',
16+
link: function(scope, element, attrs, ctrl) {
17+
window.document.querySelector('#container').textContent = ctrl.content;
18+
}
19+
};
20+
}).
21+
directive('requireTargetDirective', function() {
22+
return {
23+
controller: function() {
24+
this.content = 'requiredContent';
25+
}
26+
};
27+
});
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
describe('require parent controller on html element', function() {
4+
it('should not use the html element as the parent element', function() {
5+
6+
loadFixture('directive-require-html');
7+
8+
expect(element(by.id('container')).getText()).toContain('Controller \'requireTargetDirective\', required by directive \'requireDirective\', can\'t be found!');
9+
});
10+
});

0 commit comments

Comments
 (0)