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

Commit 1192531

Browse files
committed
fix($compile): reference correct directive name in ctreq error
Previously, ctreq would possibly reference the incorrect directive name, due to relying on a directiveName living outside of the closure which throws the exception, which can change before the call is ever made. This change saves the current value of directiveName as a property of the link function, which prevents this from occurring. Closes #7062 Closes #7067
1 parent 5dee9e4 commit 1192531

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/ng/compile.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13441344
if (pre) {
13451345
if (attrStart) pre = groupElementsLinkFnWrapper(pre, attrStart, attrEnd);
13461346
pre.require = directive.require;
1347+
pre.directiveName = directiveName;
13471348
if (newIsolateScopeDirective === directive || directive.$$isolateScope) {
13481349
pre = cloneAndAnnotateFn(pre, {isolateScope: true});
13491350
}
@@ -1352,6 +1353,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13521353
if (post) {
13531354
if (attrStart) post = groupElementsLinkFnWrapper(post, attrStart, attrEnd);
13541355
post.require = directive.require;
1356+
post.directiveName = directiveName;
13551357
if (newIsolateScopeDirective === directive || directive.$$isolateScope) {
13561358
post = cloneAndAnnotateFn(post, {isolateScope: true});
13571359
}
@@ -1360,7 +1362,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13601362
}
13611363

13621364

1363-
function getControllers(require, $element, elementControllers) {
1365+
function getControllers(directiveName, require, $element, elementControllers) {
13641366
var value, retrievalMethod = 'data', optional = false;
13651367
if (isString(require)) {
13661368
while((value = require.charAt(0)) == '^' || value == '?') {
@@ -1386,7 +1388,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
13861388
} else if (isArray(require)) {
13871389
value = [];
13881390
forEach(require, function(require) {
1389-
value.push(getControllers(require, $element, elementControllers));
1391+
value.push(getControllers(directiveName, require, $element, elementControllers));
13901392
});
13911393
}
13921394
return value;
@@ -1529,7 +1531,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
15291531
try {
15301532
linkFn = preLinkFns[i];
15311533
linkFn(linkFn.isolateScope ? isolateScope : scope, $element, attrs,
1532-
linkFn.require && getControllers(linkFn.require, $element, elementControllers), transcludeFn);
1534+
linkFn.require && getControllers(linkFn.directiveName, linkFn.require, $element, elementControllers), transcludeFn);
15331535
} catch (e) {
15341536
$exceptionHandler(e, startingTag($element));
15351537
}
@@ -1549,7 +1551,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
15491551
try {
15501552
linkFn = postLinkFns[i];
15511553
linkFn(linkFn.isolateScope ? isolateScope : scope, $element, attrs,
1552-
linkFn.require && getControllers(linkFn.require, $element, elementControllers), transcludeFn);
1554+
linkFn.require && getControllers(linkFn.directiveName, linkFn.require, $element, elementControllers), transcludeFn);
15531555
} catch (e) {
15541556
$exceptionHandler(e, startingTag($element));
15551557
}

test/ng/compileSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -3509,6 +3509,27 @@ describe('$compile', function() {
35093509
expect(element.text()).toBe('Hello');
35103510
});
35113511
});
3512+
3513+
3514+
it('should throw ctreq with correct directive name, regardless of order', function() {
3515+
module(function($compileProvider) {
3516+
$compileProvider.directive('aDir', valueFn({
3517+
restrict: "E",
3518+
require: "ngModel",
3519+
link: noop
3520+
}));
3521+
});
3522+
inject(function($compile, $rootScope) {
3523+
expect(function() {
3524+
// a-dir will cause a ctreq error to be thrown. Previously, the error would reference
3525+
// the last directive in the chain (which in this case would be ngClick), based on
3526+
// priority and alphabetical ordering. This test verifies that the ordering does not
3527+
// affect which directive is referenced in the minErr message.
3528+
element = $compile('<a-dir ng-click="foo=bar"></a-dir>')($rootScope);
3529+
}).toThrowMinErr('$compile', 'ctreq',
3530+
"Controller 'ngModel', required by directive 'aDir', can't be found!");
3531+
});
3532+
});
35123533
});
35133534

35143535

0 commit comments

Comments
 (0)