Skip to content

Commit

Permalink
fix(ngTransclude): fix case where ngTransclude attribute value equals…
Browse files Browse the repository at this point in the history
… its key

Some preprocessors such as Jade will automatically provide a value for an attribute
rather than leave it empty. E.g. `<div ng-transclude="ng-transclude">`.
In these situations we still want to use the default transclusion slot.

Closes angular#12934
Closes angular#13383
  • Loading branch information
petebacondarwin committed Nov 26, 2015
1 parent b2a937d commit 7ddbc9a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ng/directive/ngTransclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*
* @element ANY
*
* @param {string} ngTransclude|ngTranscludeSlot the name of the slot to insert at this point. If this is not provided or empty then
* the default slot is used.
* @param {string} ngTransclude|ngTranscludeSlot the name of the slot to insert at this point. If this is not provided, is empty
* or its value is the same as the name of the attribute then the default slot is used.
*
* @example
* ### Default transclusion
Expand Down Expand Up @@ -113,6 +113,12 @@ var ngTranscludeDirective = ngDirective({
restrict: 'EAC',
link: function($scope, $element, $attrs, controller, $transclude) {

if ($attrs.ngTransclude === $attrs.$attr.ngTransclude) {
// If the attribute is of the form: `ng-transclude="ng-transclude"`
// then treat it like the default
$attrs.ngTransclude = '';
}

function ngTranscludeCloneAttachFn(clone) {
$element.empty();
$element.append(clone);
Expand Down
34 changes: 34 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7680,6 +7680,40 @@ describe('$compile', function() {
});
});

it('should use the default transclusion slot if the ng-transclude attribute has the same value as its key', function() {
module(function() {
directive('minionComponent', function() {
return {
restrict: 'E',
scope: {},
transclude: {},
template:
'<div class="a" ng-transclude="ng-transclude"></div>' +
'<div class="b" ng:transclude="ng:transclude"></div>' +
'<div class="c" data-ng-transclude="data-ng-transclude"></div>'
};
});
});
inject(function($rootScope, $compile) {
element = $compile(
'<minion-component>' +
'<span>stuart</span>' +
'<span>bob</span>' +
'<span>kevin</span>' +
'</minion-component>')($rootScope);
$rootScope.$apply();
var a = element.children().eq(0);
var b = element.children().eq(1);
var c = element.children().eq(2);
expect(a).toHaveClass('a');
expect(b).toHaveClass('b');
expect(c).toHaveClass('c');
expect(a.text()).toEqual('stuartbobkevin');
expect(b.text()).toEqual('stuartbobkevin');
expect(c.text()).toEqual('stuartbobkevin');
});
});

it('should transclude elements to an `ng-transclude` with a matching transclusion slot name', function() {
module(function() {
directive('minionComponent', function() {
Expand Down

0 comments on commit 7ddbc9a

Please sign in to comment.