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

Commit

Permalink
fix(typeahead): allow parent to be required
Browse files Browse the repository at this point in the history
- Change to insert content to DOM before compiling to preserve requiring parent controllers

Closes #4800
Fixes #2679
  • Loading branch information
wesleycho committed Nov 2, 2015
1 parent 8637afc commit 3aa41f0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
46 changes: 46 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,3 +1243,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 @@ -573,9 +573,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

0 comments on commit 3aa41f0

Please sign in to comment.