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

Commit a40c3fb

Browse files
feat(typeahead): support the editable property
Closes #269
1 parent 929a46f commit a40c3fb

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/typeahead/test/typeahead.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,18 @@ describe('typeahead tests', function () {
271271
var matchHighlight = findMatches(element).find('a').html();
272272
expect(matchHighlight).toEqual('prefix<strong>fo</strong>o');
273273
});
274+
275+
it('should by default bind view value to model even if not part of matches', function () {
276+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue'></div>");
277+
changeInputValueTo(element, 'not in matches');
278+
expect($scope.result).toEqual('not in matches');
279+
});
280+
281+
it('should support the editable property to limit model bindings to matches only', function () {
282+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-editable='false'></div>");
283+
changeInputValueTo(element, 'not in matches');
284+
expect($scope.result).toEqual(undefined);
285+
});
274286
});
275287

276288
describe('selecting a match', function () {

src/typeahead/typeahead.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ angular.module('ui.bootstrap.typeahead', [])
4646
//expressions used by typeahead
4747
var parserResult = typeaheadParser.parse(attrs.typeahead);
4848

49+
//should it restrict model values to the ones selected from the popup only?
50+
var isEditable = originalScope.$eval(attrs.typeaheadEditable) !== false;
51+
4952
//create a child scope for the typeahead directive so we are not polluting original scope
5053
//with typeahead-specific data (matches, query etc.)
5154
var scope = originalScope.$new();
@@ -107,7 +110,7 @@ angular.module('ui.bootstrap.typeahead', [])
107110
}
108111
}
109112

110-
return undefined;
113+
return isEditable ? inputValue : undefined;
111114
});
112115

113116
modelCtrl.$render = function () {
@@ -151,13 +154,15 @@ angular.module('ui.bootstrap.typeahead', [])
151154

152155
} else if (evt.which === 27) {
153156
evt.stopPropagation();
154-
scope.matches = [];
157+
158+
resetMatches();
155159
scope.$digest();
156160
}
157161
});
158162

159163
$document.find('body').bind('click', function(){
160-
scope.matches = [];
164+
165+
resetMatches();
161166
scope.$digest();
162167
});
163168

0 commit comments

Comments
 (0)