This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change 11
11
* You can specify that you want to insert a named transclusion slot, instead of the default slot, by providing the slot name
12
12
* as the value of the `ng-transclude` or `ng-transclude-slot` attribute.
13
13
*
14
- * Any existing content of the element that this directive is placed on, will be removed before the transcluded content is inserted.
14
+ * For required slots and the default transclusion, existing content will be removed before the transcluded content is inserted.
15
+ *
16
+ * For optional slots, existing content is left in place, if the slot was not filled.
15
17
*
16
18
* @element ANY
17
19
*
81
83
restrict: 'E',
82
84
transclude: {
83
85
'paneTitle': '?title',
84
- 'paneBody': 'body'
86
+ 'paneBody': 'body',
87
+ 'paneFooter': '?footer'
85
88
},
86
89
template: '<div style="border: 1px solid black;">' +
87
90
'<div ng-transclude="title" style="background-color: gray"></div>' +
88
91
'<div ng-transclude="body"></div>' +
92
+ '<div ng-transclude="footer" style="background-color: gray">Default Footer</div>' +
89
93
'</div>'
90
94
};
91
95
})
@@ -132,7 +136,12 @@ var ngTranscludeDirective = ngDirective({
132
136
startingTag ( $element ) ) ;
133
137
}
134
138
135
- $transclude ( ngTranscludeCloneAttachFn , null , $attrs . ngTransclude || $attrs . ngTranscludeSlot ) ;
139
+ // If there is no slot name defined or the slot name is not optional
140
+ // then transclude the slot
141
+ var slotName = $attrs . ngTransclude || $attrs . ngTranscludeSlot ;
142
+ if ( ! slotName || $transclude . $slots [ slotName ] !== null ) {
143
+ $transclude ( ngTranscludeCloneAttachFn , null , slotName ) ;
144
+ }
136
145
}
137
146
} ) ;
138
147
Original file line number Diff line number Diff line change @@ -8030,6 +8030,37 @@ describe('$compile', function() {
8030
8030
expect ( capturedTranscludeFn . $slots . bossSlot ) . toBe ( null ) ;
8031
8031
} ) ;
8032
8032
} ) ;
8033
+
8034
+ it ( 'should not overwrite the contents of an `ng-transclude` element, if the matching optional slot is not filled' , function ( ) {
8035
+ module ( function ( ) {
8036
+ directive ( 'minionComponent' , function ( ) {
8037
+ return {
8038
+ restrict : 'E' ,
8039
+ scope : { } ,
8040
+ transclude : {
8041
+ minion : 'minionSlot' ,
8042
+ boss : '?bossSlot'
8043
+ } ,
8044
+ template :
8045
+ '<div class="boss" ng-transclude="bossSlot">default boss content</div>' +
8046
+ '<div class="minion" ng-transclude="minionSlot">default minion content</div>' +
8047
+ '<div class="other" ng-transclude>default content</div>'
8048
+ } ;
8049
+ } ) ;
8050
+ } ) ;
8051
+ inject ( function ( $rootScope , $compile ) {
8052
+ element = $compile (
8053
+ '<minion-component>' +
8054
+ '<minion>stuart</minion>' +
8055
+ '<span>dorothy</span>' +
8056
+ '<minion>kevin</minion>' +
8057
+ '</minion-component>' ) ( $rootScope ) ;
8058
+ $rootScope . $apply ( ) ;
8059
+ expect ( element . children ( ) . eq ( 0 ) . text ( ) ) . toEqual ( 'default boss content' ) ;
8060
+ expect ( element . children ( ) . eq ( 1 ) . text ( ) ) . toEqual ( 'stuartkevin' ) ;
8061
+ expect ( element . children ( ) . eq ( 2 ) . text ( ) ) . toEqual ( 'dorothy' ) ;
8062
+ } ) ;
8063
+ } ) ;
8033
8064
} ) ;
8034
8065
8035
8066
You can’t perform that action at this time.
0 commit comments