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

Commit 5a1a6b8

Browse files
jankucabtford
authored andcommitted
fix(ngTransclude): detect ngTranslude usage without a transclusion directive
Closes #3759
1 parent 742271f commit 5a1a6b8

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@ngdoc error
2+
@name ngTransclude:orphan
3+
@fullName Orphan ngTransclude Directive
4+
@description
5+
6+
Occurs when an `ngTransclude` occurs without a transcluded ancesstor element.
7+
8+
This error often occurs when you have forgotten to set `transclude: true` in some directive definition, and then used `ngTranslude` in the driective's template.
9+
10+
To resolve, either remove the offending `ngTransclude` or check that `transclude: true` is included in the intended directive definition.
11+
12+
Consult the API documentation for {@link guide/directive writing directives} to learn more.

src/ng/directive/ngTransclude.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@
5252
*
5353
*/
5454
var ngTranscludeDirective = ngDirective({
55-
controller: ['$transclude', function($transclude) {
55+
controller: ['$element', '$transclude', function($element, $transclude) {
56+
if (!$transclude) {
57+
throw minErr('ngTransclude')('orphan',
58+
'Illegal use of ngTransclude directive in the template! ' +
59+
'No parent directive that requires a transclusion found. ' +
60+
'Element: {0}',
61+
startingTag($element));
62+
}
63+
5664
// remember the transclusion fn but call it during linking so that we don't process transclusion before directives on
5765
// the parent element even when the transclusion replaces the current element. (we can't use priority here because
5866
// that applies only to compile fns and not controllers

test/ng/compileSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,22 @@ describe('$compile', function() {
28342834
});
28352835

28362836

2837+
it('should throw on an ng-translude element inside no transclusion directive', function() {
2838+
inject(function ($rootScope, $compile) {
2839+
// we need to do this because different browsers print empty attributres differently
2840+
try {
2841+
$compile('<div><div ng-transclude></div></div>')($rootScope);
2842+
} catch(e) {
2843+
expect(e.message).toMatch(new RegExp(
2844+
'^\\\[ngTransclude:orphan\\\] ' +
2845+
'Illegal use of ngTransclude directive in the template! ' +
2846+
'No parent directive that requires a transclusion found\. ' +
2847+
'Element: <div ng-transclude.+'));
2848+
}
2849+
});
2850+
});
2851+
2852+
28372853
it('should make the result of a transclusion available to the parent directive in post-linking phase (template)',
28382854
function() {
28392855
module(function() {

0 commit comments

Comments
 (0)