Skip to content

Commit bf7eba7

Browse files
committed
The ngTransclude Directive
1 parent 8b18449 commit bf7eba7

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/angular_public.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ function publishExternalAPI() {
1313
ngModule.provider('$compile', $CompileProvider);
1414
ngModule.provider('$controller', $ControllerProvider);
1515
ngModule.directive('ngController', ngControllerDirective);
16+
ngModule.directive('ngTransclude', ngTranscludeDirective);
1617
}

src/directives/ng_transclude.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var ngTranscludeDirective = function() {
2+
'use strict';
3+
4+
return {
5+
restrict: 'EAC',
6+
link: function(scope, element, attrs, ctrl, transclude) {
7+
transclude(function(clone) {
8+
element.empty();
9+
element.append(clone);
10+
});
11+
}
12+
};
13+
14+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
describe('ngTransclude', function() {
2+
3+
beforeEach(function() {
4+
delete window.angular;
5+
publishExternalAPI();
6+
});
7+
8+
function createInjectorWithTranscluderTemplate(template) {
9+
return createInjector(['ng', function($compileProvider) {
10+
$compileProvider.directive('myTranscluder', function() {
11+
return {
12+
transclude: true,
13+
template: template
14+
};
15+
});
16+
}]);
17+
}
18+
19+
it('transcludes the parent directive transclusion', function() {
20+
var injector = createInjectorWithTranscluderTemplate(
21+
'<div ng-transclude></div>'
22+
);
23+
injector.invoke(function($compile, $rootScope) {
24+
var el = $('<div my-transcluder>Hello</div>');
25+
$compile(el)($rootScope);
26+
expect(el.find('> [ng-transclude]').html()).toEqual('Hello');
27+
});
28+
});
29+
30+
it('empties existing contents', function() {
31+
var injector = createInjectorWithTranscluderTemplate(
32+
'<div ng-transclude>Existing contents</div>'
33+
);
34+
injector.invoke(function($compile, $rootScope) {
35+
var el = $('<div my-transcluder>Hello</div>');
36+
$compile(el)($rootScope);
37+
expect(el.find('> [ng-transclude]').html()).toEqual('Hello');
38+
});
39+
});
40+
41+
it('may be used as element', function() {
42+
var injector = createInjectorWithTranscluderTemplate(
43+
'<ng-transclude>Existing contents</ng-transclude>'
44+
);
45+
injector.invoke(function($compile, $rootScope) {
46+
var el = $('<div my-transcluder>Hello</div>');
47+
$compile(el)($rootScope);
48+
expect(el.find('> ng-transclude').html()).toEqual('Hello');
49+
});
50+
});
51+
52+
it('may be used as class', function() {
53+
var injector = createInjectorWithTranscluderTemplate(
54+
'<div class="ng-transclude">Existing contents</div>'
55+
);
56+
injector.invoke(function($compile, $rootScope) {
57+
var el = $('<div my-transcluder>Hello</div>');
58+
$compile(el)($rootScope);
59+
expect(el.find('> .ng-transclude').html()).toEqual('Hello');
60+
});
61+
});
62+
63+
});

0 commit comments

Comments
 (0)