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

fix($compile): use correct parent element when requiring on html element #16647

Merged
merged 1 commit into from
Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2960,7 +2960,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

if (!value) {
var dataName = '$' + name + 'Controller';
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);

if (inheritType === '^^' && $element[0] && $element[0].nodeType === NODE_TYPE_DOCUMENT) {
// inheritedData() uses the documentElement when it finds the document, so we would
// require from the element itself.
value = null;
} else {
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
}
}

if (!value && !optional) {
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/fixtures/directive-require-html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html ng-app="test" require-directive require-target-directive>
<body>
<div id="container"></div>
<script src="angular.js"></script>
<script src="script.js"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions test/e2e/fixtures/directive-require-html/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

angular.
module('test', []).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if you overwrote $exceptionHandler to put the error message into a DOM element.
Then you could easily read the error in the test (and verify that it is what you expect).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, didn't think of that

provider('$exceptionHandler', /** @this */ function() {
this.$get = [function() {
return function(error) {
window.document.querySelector('#container').textContent = error && error.message;
};
}];
}).

directive('requireDirective', function() {
return {
require: '^^requireTargetDirective',
link: function(scope, element, attrs, ctrl) {
window.document.querySelector('#container').textContent = ctrl.content;
}
};
}).
directive('requireTargetDirective', function() {
return {
controller: function() {
this.content = 'requiredContent';
}
};
});

10 changes: 10 additions & 0 deletions test/e2e/tests/directive-require-html.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

describe('require parent controller on html element', function() {
it('should not use the html element as the parent element', function() {

loadFixture('directive-require-html');

expect(element(by.id('container')).getText()).toContain('Controller \'requireTargetDirective\', required by directive \'requireDirective\', can\'t be found!');
});
});