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

Commit

Permalink
fix(ngTransclude): detect ngTranslude usage without a transclusion di…
Browse files Browse the repository at this point in the history
…rective

Closes #3759
  • Loading branch information
jankuca authored and btford committed Sep 30, 2013
1 parent 742271f commit 5a1a6b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/content/error/ngTransclude/orphan.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@ngdoc error
@name ngTransclude:orphan
@fullName Orphan ngTransclude Directive
@description

Occurs when an `ngTransclude` occurs without a transcluded ancesstor element.

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.

To resolve, either remove the offending `ngTransclude` or check that `transclude: true` is included in the intended directive definition.

Consult the API documentation for {@link guide/directive writing directives} to learn more.
10 changes: 9 additions & 1 deletion src/ng/directive/ngTransclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@
*
*/
var ngTranscludeDirective = ngDirective({
controller: ['$transclude', function($transclude) {
controller: ['$element', '$transclude', function($element, $transclude) {
if (!$transclude) {
throw minErr('ngTransclude')('orphan',
'Illegal use of ngTransclude directive in the template! ' +
'No parent directive that requires a transclusion found. ' +
'Element: {0}',
startingTag($element));
}

// remember the transclusion fn but call it during linking so that we don't process transclusion before directives on
// the parent element even when the transclusion replaces the current element. (we can't use priority here because
// that applies only to compile fns and not controllers
Expand Down
16 changes: 16 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,22 @@ describe('$compile', function() {
});


it('should throw on an ng-translude element inside no transclusion directive', function() {
inject(function ($rootScope, $compile) {
// we need to do this because different browsers print empty attributres differently
try {
$compile('<div><div ng-transclude></div></div>')($rootScope);
} catch(e) {
expect(e.message).toMatch(new RegExp(
'^\\\[ngTransclude:orphan\\\] ' +
'Illegal use of ngTransclude directive in the template! ' +
'No parent directive that requires a transclusion found\. ' +
'Element: <div ng-transclude.+'));
}
});
});


it('should make the result of a transclusion available to the parent directive in post-linking phase (template)',
function() {
module(function() {
Expand Down

0 comments on commit 5a1a6b8

Please sign in to comment.