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

Commit

Permalink
fix(typeahead): add support for ngModelOptions getterSetter
Browse files Browse the repository at this point in the history
- Add support for getterSetter with ngModelOptions

Closes #3865
Fixes #3823
  • Loading branch information
wesleycho committed Aug 31, 2015
1 parent 56bdb1c commit ccaa627
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,19 @@ describe('typeahead tests', function() {

expect($scope.test.typeahead.$error.parse).toBeUndefined();
});

it('issue #3823 - should support ng-model-options getterSetter', function() {
function resultSetter(state) {
return state;
}
$scope.result = resultSetter;
var element = prepareInputEl('<div><input name="typeahead" ng-model="result" ng-model-options="{getterSetter: true}" typeahead="state as state.name for state in states | filter:$viewValue" typeahead-editable="false"></div>');

changeInputValueTo(element, 'Alaska');
triggerKeyDown(element, 13);

expect($scope.result).toBe(resultSetter);
});
});

describe('input formatting', function() {
Expand Down
21 changes: 16 additions & 5 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
var eventDebounceTime = 200;

return {
require: 'ngModel',
link: function(originalScope, element, attrs, modelCtrl) {
require: ['ngModel', '^?ngModelOptions'],
link: function(originalScope, element, attrs, ctrls) {
var modelCtrl = ctrls[0];
var ngModelOptions = ctrls[1];
//SUPPORTED ATTRIBUTES (OPTIONS)

//minimal no of characters that needs to be entered before typeahead kicks-in
Expand Down Expand Up @@ -74,7 +76,16 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
//INTERNAL VARIABLES

//model setter executed upon match selection
var $setModelValue = $parse(attrs.ngModel).assign;
var parsedModel = $parse(attrs.ngModel);
var invokeModelSetter = $parse(attrs.ngModel + '($$$p)');
var $setModelValue = function(scope, newValue) {
if (angular.isFunction(parsedModel(originalScope)) &&
ngModelOptions && ngModelOptions.$options && ngModelOptions.$options.getterSetter) {
return invokeModelSetter(scope, {$$$p: newValue});
} else {
return parsedModel.assign(scope, newValue);
}
};

//expressions used by typeahead
var parserResult = typeaheadParser.parse(attrs.typeahead);
Expand All @@ -89,8 +100,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
//create a child scope for the typeahead directive so we are not polluting original scope
//with typeahead-specific data (matches, query etc.)
var scope = originalScope.$new();
var offDestroy = originalScope.$on('$destroy', function(){
scope.$destroy();
var offDestroy = originalScope.$on('$destroy', function() {
scope.$destroy();
});
scope.$on('$destroy', offDestroy);

Expand Down

0 comments on commit ccaa627

Please sign in to comment.