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

Commit a5bafe6

Browse files
damon.friendshipwesleycho
damon.friendship
authored andcommitted
feat(typeahead): add dynamic toggling of Editable
- Allow user to toggle value of `typeaheadEditable` dynamically Closes #2638 Closes #4820
1 parent 1f43cdf commit a5bafe6

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/typeahead/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The typeahead directives provide several attributes:
2727
* `typeahead-append-to`
2828
_(Defaults: null)_ : Should the typeahead popup be appended to an element instead of the parent element?
2929

30-
* `typeahead-editable`
30+
* `typeahead-editable` <i class="glyphicon glyphicon-eye-open"></i>
3131
_(Defaults: true)_ :
3232
Should it restrict model values to the ones selected from the popup only ?
3333

src/typeahead/test/typeahead.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,24 @@ describe('typeahead tests', function() {
295295
expect(inputEl.val()).toEqual('bar');
296296
});
297297

298+
it('should support changing the editable property to limit model bindings to matches only', function() {
299+
$scope.isEditable = true;
300+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="isEditable"></div>');
301+
$scope.isEditable = false;
302+
$scope.$digest();
303+
changeInputValueTo(element, 'not in matches');
304+
expect($scope.result).toEqual(undefined);
305+
});
306+
307+
it('should support changing the editable property to bind view value to model even if not part of matches', function() {
308+
$scope.isEditable = false;
309+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="isEditable"></div>');
310+
$scope.isEditable = true;
311+
$scope.$digest();
312+
changeInputValueTo(element, 'not in matches');
313+
expect($scope.result).toEqual('not in matches');
314+
});
315+
298316
it('should bind loading indicator expression', inject(function($timeout) {
299317
$scope.isLoading = false;
300318
$scope.loadMatches = function(viewValue) {

src/typeahead/typeahead.js

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
4444

4545
//should it restrict model values to the ones selected from the popup only?
4646
var isEditable = originalScope.$eval(attrs.typeaheadEditable) !== false;
47+
originalScope.$watch(attrs.typeaheadEditable, function (newVal) {
48+
isEditable = newVal !== false;
49+
});
4750

4851
//binding to a variable that indicates if matches are being retrieved asynchronously
4952
var isLoadingSetter = $parse(attrs.typeaheadLoading).assign || angular.noop;

0 commit comments

Comments
 (0)