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

fix(typeahead): allow parent to be required #4800

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,3 +1242,49 @@ describe('typeahead tests', function() {
});
});
});

describe('typeahead tests', function() {
it('should allow directives in template to require parent controller', function() {
module('ui.bootstrap.typeahead');
module('ngSanitize');
module('template/typeahead/typeahead-popup.html');
module(function($compileProvider) {
$compileProvider
.directive('uibCustomParent', function() {
return {
controller: function() {
this.text = 'foo';
}
};
})
.directive('uibCustomDirective', function() {
return {
require: '^uibCustomParent',
link: function(scope, element, attrs, ctrl) {
scope.text = ctrl.text;
}
};
});
});

inject(function($compile, $rootScope, $sniffer, $templateCache) {
var element;
var $scope = $rootScope.$new();
$templateCache.put('template/typeahead/typeahead-match.html', '<div uib-custom-directive>{{text}}</div>');
$scope.states = [
{code: 'AL', name: 'Alaska'},
{code: 'CL', name: 'California'}
];

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);
$rootScope.$digest();

var inputEl = element.find('input');
inputEl.val('Al');
inputEl.trigger($sniffer.hasEvent('input') ? 'input' : 'change');
$scope.$digest();

expect(element.find('ul.dropdown-menu li').eq(0).find('[uib-custom-directive]').text()).toEqual('foo');
});
});
});
6 changes: 3 additions & 3 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
link: function(scope, element, attrs) {
var tplUrl = $parse(attrs.templateUrl)(scope.$parent) || 'template/typeahead/typeahead-match.html';
$templateRequest(tplUrl).then(function(tplContent) {
$compile(tplContent.trim())(scope, function(clonedElement) {
element.replaceWith(clonedElement);
});
var tplEl = angular.element(tplContent.trim());
element.replaceWith(tplEl);
$compile(tplEl)(scope);
});
}
};
Expand Down