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

fix($compile): reference correct directive name in ctreq error #7067

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 6 additions & 4 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (pre) {
if (attrStart) pre = groupElementsLinkFnWrapper(pre, attrStart, attrEnd);
pre.require = directive.require;
pre.directiveName = directiveName;
if (newIsolateScopeDirective === directive || directive.$$isolateScope) {
pre = cloneAndAnnotateFn(pre, {isolateScope: true});
}
Expand All @@ -1352,6 +1353,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (post) {
if (attrStart) post = groupElementsLinkFnWrapper(post, attrStart, attrEnd);
post.require = directive.require;
post.directiveName = directiveName;
if (newIsolateScopeDirective === directive || directive.$$isolateScope) {
post = cloneAndAnnotateFn(post, {isolateScope: true});
}
Expand All @@ -1360,7 +1362,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}


function getControllers(require, $element, elementControllers) {
function getControllers(directiveName, require, $element, elementControllers) {
var value, retrievalMethod = 'data', optional = false;
if (isString(require)) {
while((value = require.charAt(0)) == '^' || value == '?') {
Expand All @@ -1386,7 +1388,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
} else if (isArray(require)) {
value = [];
forEach(require, function(require) {
value.push(getControllers(require, $element, elementControllers));
value.push(getControllers(directiveName, require, $element, elementControllers));
});
}
return value;
Expand Down Expand Up @@ -1529,7 +1531,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
try {
linkFn = preLinkFns[i];
linkFn(linkFn.isolateScope ? isolateScope : scope, $element, attrs,
linkFn.require && getControllers(linkFn.require, $element, elementControllers), transcludeFn);
linkFn.require && getControllers(linkFn.directiveName, linkFn.require, $element, elementControllers), transcludeFn);
} catch (e) {
$exceptionHandler(e, startingTag($element));
}
Expand All @@ -1549,7 +1551,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
try {
linkFn = postLinkFns[i];
linkFn(linkFn.isolateScope ? isolateScope : scope, $element, attrs,
linkFn.require && getControllers(linkFn.require, $element, elementControllers), transcludeFn);
linkFn.require && getControllers(linkFn.directiveName, linkFn.require, $element, elementControllers), transcludeFn);
} catch (e) {
$exceptionHandler(e, startingTag($element));
}
Expand Down
21 changes: 21 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3509,6 +3509,27 @@ describe('$compile', function() {
expect(element.text()).toBe('Hello');
});
});


it('should throw ctreq with correct directive name, regardless of order', function() {
module(function($compileProvider) {
$compileProvider.directive('aDir', valueFn({
restrict: "E",
require: "ngModel",
link: noop
}));
});
inject(function($compile, $rootScope) {
expect(function() {
// a-dir will cause a ctreq error to be thrown. Previously, the error would reference
// the last directive in the chain (which in this case would be ngClick), based on
// priority and alphabetical ordering. This test verifies that the ordering does not
// affect which directive is referenced in the minErr message.
element = $compile('<a-dir ng-click="foo=bar"></a-dir>')($rootScope);
}).toThrowMinErr('$compile', 'ctreq',
"Controller 'ngModel', required by directive 'aDir', can't be found!");
});
});
});


Expand Down