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

Commit 3aa41f0

Browse files
committed
fix(typeahead): allow parent to be required
- Change to insert content to DOM before compiling to preserve requiring parent controllers Closes #4800 Fixes #2679
1 parent 8637afc commit 3aa41f0

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/typeahead/test/typeahead.spec.js

+46
Original file line numberDiff line numberDiff line change
@@ -1243,3 +1243,49 @@ describe('typeahead tests', function() {
12431243
});
12441244
});
12451245
});
1246+
1247+
describe('typeahead tests', function() {
1248+
it('should allow directives in template to require parent controller', function() {
1249+
module('ui.bootstrap.typeahead');
1250+
module('ngSanitize');
1251+
module('template/typeahead/typeahead-popup.html');
1252+
module(function($compileProvider) {
1253+
$compileProvider
1254+
.directive('uibCustomParent', function() {
1255+
return {
1256+
controller: function() {
1257+
this.text = 'foo';
1258+
}
1259+
};
1260+
})
1261+
.directive('uibCustomDirective', function() {
1262+
return {
1263+
require: '^uibCustomParent',
1264+
link: function(scope, element, attrs, ctrl) {
1265+
scope.text = ctrl.text;
1266+
}
1267+
};
1268+
});
1269+
});
1270+
1271+
inject(function($compile, $rootScope, $sniffer, $templateCache) {
1272+
var element;
1273+
var $scope = $rootScope.$new();
1274+
$templateCache.put('template/typeahead/typeahead-match.html', '<div uib-custom-directive>{{text}}</div>');
1275+
$scope.states = [
1276+
{code: 'AL', name: 'Alaska'},
1277+
{code: 'CL', name: 'California'}
1278+
];
1279+
1280+
element = $compile('<div uib-custom-parent><input ng-model="result" uib-typeahead="state.code as state.name + state.code for state in states"></div>')($scope);
1281+
$rootScope.$digest();
1282+
1283+
var inputEl = element.find('input');
1284+
inputEl.val('Al');
1285+
inputEl.trigger($sniffer.hasEvent('input') ? 'input' : 'change');
1286+
$scope.$digest();
1287+
1288+
expect(element.find('ul.dropdown-menu li').eq(0).find('[uib-custom-directive]').text()).toEqual('foo');
1289+
});
1290+
});
1291+
});

src/typeahead/typeahead.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
573573
link: function(scope, element, attrs) {
574574
var tplUrl = $parse(attrs.templateUrl)(scope.$parent) || 'template/typeahead/typeahead-match.html';
575575
$templateRequest(tplUrl).then(function(tplContent) {
576-
$compile(tplContent.trim())(scope, function(clonedElement) {
577-
element.replaceWith(clonedElement);
578-
});
576+
var tplEl = angular.element(tplContent.trim());
577+
element.replaceWith(tplEl);
578+
$compile(tplEl)(scope);
579579
});
580580
}
581581
};

0 commit comments

Comments
 (0)