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

Commit 5294e53

Browse files
feat($compile): exponse isSlotFilled() method on $transclude function
1 parent 10000d6 commit 5294e53

File tree

3 files changed

+10
-55
lines changed

3 files changed

+10
-55
lines changed

src/ng/compile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21522152
transcludeFn = controllersBoundTransclude;
21532153
transcludeFn.$$boundTransclude = boundTranscludeFn;
21542154
// expose the slots on the `$transclude` function
2155-
transcludeFn.$slots = boundTranscludeFn.$$slots;
2155+
transcludeFn.isSlotFilled = function(slotName) {
2156+
return !!boundTranscludeFn.$$slots[slotName];
2157+
};
21562158
}
21572159

21582160
if (controllerDirectives) {

src/ng/directive/ngTransclude.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ var ngTranscludeDirective = ngDirective({
139139
// If there is no slot name defined or the slot name is not optional
140140
// then transclude the slot
141141
var slotName = $attrs.ngTransclude || $attrs.ngTranscludeSlot;
142-
if (!slotName || $transclude.$slots[slotName] !== null) {
143-
$transclude(ngTranscludeCloneAttachFn, null, slotName);
144-
}
142+
$transclude(ngTranscludeCloneAttachFn, null, slotName);
145143
}
146144
});
147145

test/ng/compileSpec.js

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7948,7 +7948,7 @@ ddescribe('$compile', function() {
79487948
});
79497949

79507950

7951-
it('should provide the elements marked with matching transclude elements as additional transclude functions on the $slots property', function() {
7951+
it('should return true from `isSlotFilled(slotName) for slots that have content in the transclusion', function() {
79527952
var capturedTranscludeFn;
79537953
module(function() {
79547954
directive('minionComponent', function() {
@@ -7957,7 +7957,7 @@ ddescribe('$compile', function() {
79577957
scope: {},
79587958
transclude: {
79597959
minionSlot: 'minion',
7960-
bossSlot: 'boss'
7960+
bossSlot: '?boss'
79617961
},
79627962
template:
79637963
'<div class="boss" ng-transclude="bossSlot"></div>' +
@@ -7975,59 +7975,14 @@ ddescribe('$compile', function() {
79757975
' <minion>stuart</minion>' +
79767976
' <minion>bob</minion>' +
79777977
' <span>dorothy</span>' +
7978-
' <boss>gru</boss>' +
79797978
'</minion-component>')($rootScope);
79807979
$rootScope.$apply();
79817980

7982-
var minionTranscludeFn = capturedTranscludeFn.$slots['minionSlot'];
7983-
var minions = minionTranscludeFn();
7984-
expect(minions[0].outerHTML).toEqual('<minion class="ng-scope">stuart</minion>');
7985-
expect(minions[1].outerHTML).toEqual('<minion class="ng-scope">bob</minion>');
7986-
7987-
var scope = element.scope();
7988-
7989-
var minionScope = jqLite(minions[0]).scope();
7990-
expect(minionScope.$parent).toBe(scope);
7991-
7992-
var bossTranscludeFn = capturedTranscludeFn.$slots['bossSlot'];
7993-
var boss = bossTranscludeFn();
7994-
expect(boss[0].outerHTML).toEqual('<boss class="ng-scope">gru</boss>');
7995-
7996-
var bossScope = jqLite(boss[0]).scope();
7997-
expect(bossScope.$parent).toBe(scope);
7998-
7999-
expect(bossScope).not.toBe(minionScope);
8000-
8001-
dealoc(boss);
8002-
dealoc(minions);
8003-
});
8004-
});
7981+
var hasMinions = capturedTranscludeFn.isSlotFilled('minionSlot');
7982+
var hasBosses = capturedTranscludeFn.isSlotFilled('bossSlot');
80057983

8006-
it('should set unfilled optional transclude slots to `null` in the $transclude.$slots property', function() {
8007-
var capturedTranscludeFn;
8008-
module(function() {
8009-
directive('minionComponent', function() {
8010-
return {
8011-
restrict: 'E',
8012-
scope: {},
8013-
transclude: {
8014-
minionSlot: 'minion',
8015-
bossSlot: '?boss'
8016-
},
8017-
link: function(s, e, a, c, transcludeFn) {
8018-
capturedTranscludeFn = transcludeFn;
8019-
}
8020-
};
8021-
});
8022-
});
8023-
inject(function($rootScope, $compile) {
8024-
element = $compile(
8025-
'<minion-component>' +
8026-
'<minion>stuart</minion>' +
8027-
'<span>dorothy</span>' +
8028-
'</minion-component>')($rootScope);
8029-
expect(capturedTranscludeFn.$slots.minionSlot).toEqual(jasmine.any(Function));
8030-
expect(capturedTranscludeFn.$slots.bossSlot).toBe(null);
7984+
expect(hasMinions).toBe(true);
7985+
expect(hasBosses).toBe(false);
80317986
});
80327987
});
80337988

0 commit comments

Comments
 (0)