Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit fe679bf

Browse files
committed
Merge pull request #151 from angular-ui/fix-transclude-scope
fix(transclude): compile transclude elements with correct scope
2 parents 59f6e14 + ee6dc45 commit fe679bf

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

src/select.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,11 @@
599599
.attr('ng-mouseenter', '$select.setActiveItem('+$select.parserResult.itemName +')')
600600
.attr('ng-click', '$select.select(' + $select.parserResult.itemName + ')');
601601

602-
transcludeFn(function(clone) {
603-
var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner');
604-
if (rowsInner.length !== 1)
605-
throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
602+
var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner');
603+
if (rowsInner.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
604+
rowsInner.attr('uis-transclude-append', ''); //Adding uisTranscludeAppend directive to row element after choices element has ngRepeat
606605

607-
rowsInner.append(clone);
608-
$compile(element)(scope);
609-
});
606+
$compile(element, transcludeFn)(scope); //Passing current transcludeFn to be able to append elements correctly from uisTranscludeAppend
610607

611608
scope.$watch('$select.search', function() {
612609
$select.activeIndex = 0;
@@ -622,7 +619,15 @@
622619
}
623620
};
624621
}])
625-
622+
.directive('uisTranscludeAppend', function () {
623+
return {
624+
link: function (scope, element, attrs, ctrl, transclude) {
625+
transclude(scope, function (clone) {
626+
element.append(clone);
627+
});
628+
}
629+
};
630+
})
626631
.directive('uiSelectMatch', ['uiSelectConfig', function(uiSelectConfig) {
627632
return {
628633
restrict: 'EA',

test/select.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,46 @@ describe('ui-select tests', function() {
510510

511511
});
512512

513+
it('should append/transclude content (with correct scope) that users add at <match> tag', function () {
514+
515+
var el = compileTemplate(
516+
'<ui-select ng-model="selection.selected"> \
517+
<ui-select-match> \
518+
<span ng-if="$select.selected.name!==\'Wladimir\'">{{$select.selected.name}}</span>\
519+
<span ng-if="$select.selected.name===\'Wladimir\'">{{$select.selected.name | uppercase}}</span>\
520+
</ui-select-match> \
521+
<ui-select-choices repeat="person in people | filter: $select.search"> \
522+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
523+
</ui-select-choices> \
524+
</ui-select>'
525+
);
526+
527+
clickItem(el, 'Samantha');
528+
expect(getMatchLabel(el).trim()).toEqual('Samantha');
529+
530+
clickItem(el, 'Wladimir');
531+
expect(getMatchLabel(el).trim()).not.toEqual('Wladimir');
532+
expect(getMatchLabel(el).trim()).toEqual('WLADIMIR');
533+
534+
});
535+
it('should append/transclude content (with correct scope) that users add at <choices> tag', function () {
536+
537+
var el = compileTemplate(
538+
'<ui-select ng-model="selection.selected"> \
539+
<ui-select-match> \
540+
</ui-select-match> \
541+
<ui-select-choices repeat="person in people | filter: $select.search"> \
542+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
543+
<div ng-if="person.name==\'Wladimir\'"> \
544+
<span class="only-once">I should appear only once</span>\
545+
</div> \
546+
</ui-select-choices> \
547+
</ui-select>'
548+
);
549+
550+
expect($(el).find('.only-once').length).toEqual(1);
551+
552+
553+
});
554+
513555
});

0 commit comments

Comments
 (0)