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

Commit 91ac17c

Browse files
committedJun 8, 2013
feat(typeahead): support typeahead-on-select callback
Closes #433
1 parent 5f7f307 commit 91ac17c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
 

‎src/typeahead/test/typeahead.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,29 @@ describe('typeahead tests', function () {
369369
expect(inputEl.val()).toEqual('baz');
370370
});
371371

372+
it('should invoke select callback on select', function () {
373+
374+
$scope.states = [
375+
{code: 'AL', name: 'Alaska'},
376+
{code: 'CL', name: 'California'}
377+
];
378+
$scope.onSelect = function ($item, $model, $label) {
379+
$scope.$item = $item;
380+
$scope.$model = $model;
381+
$scope.$label = $label;
382+
};
383+
var element = prepareInputEl("<div><input ng-model='result' typeahead-on-select='onSelect($item, $model, $label)' typeahead='state.code as state.name for state in states | filter:$viewValue'></div>");
384+
var inputEl = findInput(element);
385+
386+
changeInputValueTo(element, 'Alas');
387+
triggerKeyDown(element, 13);
388+
389+
expect($scope.result).toEqual('AL');
390+
expect($scope.$item).toEqual($scope.states[0]);
391+
expect($scope.$model).toEqual('AL');
392+
expect($scope.$label).toEqual('Alaska');
393+
});
394+
372395
it('should correctly update inputs value on mapping where label is not derived from the model', function () {
373396

374397
$scope.states = [{code: 'AL', name: 'Alaska'}, {code: 'CL', name: 'California'}];

‎src/typeahead/typeahead.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
5050

5151
var isLoadingSetter = $parse(attrs.typeaheadLoading).assign || angular.noop;
5252

53+
var onSelectCallback = $parse(attrs.typeaheadOnSelect);
54+
5355
//pop-up element used to display matches
5456
var popUpEl = angular.element(
5557
"<typeahead-popup " +
@@ -144,10 +146,18 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
144146
scope.select = function (activeIdx) {
145147
//called from within the $digest() cycle
146148
var locals = {};
147-
locals[parserResult.itemName] = selected = scope.matches[activeIdx].model;
149+
var model, item;
150+
locals[parserResult.itemName] = item = selected = scope.matches[activeIdx].model;
148151

149-
modelCtrl.$setViewValue(parserResult.modelMapper(scope, locals));
152+
model = parserResult.modelMapper(scope, locals);
153+
modelCtrl.$setViewValue(model);
150154
modelCtrl.$render();
155+
156+
onSelectCallback(scope, {
157+
$item: item,
158+
$model: model,
159+
$label: parserResult.viewMapper(scope, locals)
160+
});
151161
};
152162

153163
//bind keyboard events: arrows up(38) / down(40), enter(13) and tab(9), esc(27)

0 commit comments

Comments
 (0)