@@ -337,6 +337,80 @@ We inject the $compile service and $rootScope before each jasmine test. The $com
337
337
to render the aGreatEye directive. After rendering the directive we ensure that the directive has
338
338
replaced the content and "lidless, wreathed in flame, 2 times" is present.
339
339
340
+ ### Testing Transclusion Directives
341
+
342
+ Directives that use transclusion are treated specially by the compiler. Before their compile
343
+ function is called, the contents of the directive's element are removed from the element and
344
+ provided via a transclusion function. The directive's template is then appended to the directive's
345
+ element, to which it can then insert the transcluded content into its template.
346
+
347
+
348
+ Before compilation:
349
+ ```html
350
+ <div translude-directive>
351
+ Some transcluded content
352
+ </div>
353
+ ```
354
+
355
+ After transclusion extraction:
356
+ ```html
357
+ <div transclude-directive></div>
358
+ ```
359
+
360
+ After compilation:
361
+ ```html
362
+ <div transclude-directive>
363
+ Some Template
364
+ <span ng-transclude>Some transcluded content</span>
365
+ </div>
366
+ ```
367
+
368
+ If the directive is using 'element' transclusion, the compiler will actually remove the
369
+ directive's entire element from the DOM and replace it with a comment node. The compiler then
370
+ inserts the directive's template "after" this comment node, as a sibling.
371
+
372
+ Before compilation
373
+ ```html
374
+ <div element-transclude>
375
+ Some Content
376
+ </div>
377
+ ```
378
+
379
+ After transclusion extraction
380
+ ```html
381
+ <!-- elementTransclude -->
382
+ ```
383
+
384
+ After compilation:
385
+ ```html
386
+ <!-- elementTransclude -->
387
+ <div element-transclude>
388
+ Some Template
389
+ <span ng-transclude>Some transcluded content</span>
390
+ </div>
391
+ ```
392
+
393
+ It is important to be aware of this when writing tests for directives that use 'element'
394
+ transclusion. If you place the directive on the root element of the DOM fragment that you
395
+ pass to {@link $compile}, then the DOM node returned from the linking function will be the
396
+ comment node and you will lose the ability to access the template and transcluded content.
397
+
398
+ ```javascript
399
+ var node = $compile('<div element-transclude></div>')($rootScope);
400
+ expect(node[0].nodeType).toEqual(node.COMMENT_NODE);
401
+ expect(node[1]).toBeUndefined();
402
+ ```
403
+
404
+ To cope with this you simply ensure that your 'element' transclude directive is wrapped in an
405
+ element, such as a `<div>`.
406
+
407
+ ```javascript
408
+ var node = $compile('<div><div element-transclude></div></div>')($rootScope);
409
+ var contents = node.contents();
410
+ expect(contents[0].nodeType).toEqual(node.COMMENT_NODE);
411
+ expect(contents[1].nodeType).toEqual(node.ELEMENT_NODE);
412
+ ```
413
+
340
414
### Testing Directives With External Templates
341
415
342
416
If your directive uses `templateUrl`, consider using
0 commit comments