Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular UI Bootstrap - Clear model value when a Typeahead dropdown value is not selected?

I'm trying to figure out how to have a UI.Bootstrap Typeahead (http://angular-ui.github.io/bootstrap/) only set the model value if a selection is made from the dropdown, and clear the field if no selection was made.

If there's a different control out there that does this that would work too.

The Typeahead is selecting an object and if the user just types a few characters and then leaves the field, the model value is set to the string value of the field. I can obviously test for this case but modifying the scope model value doesn't refresh the form validity. So if I have a required field I also have to set the field's validity to false.

This all seems like a lot of work that someone must have already figured out. Any suggestions?

Edit

Another thought I had was, is it best practice to separate the Typeahead from the scope values that are being submitted back to the server?

like image 568
Josh Russo Avatar asked Sep 03 '26 02:09

Josh Russo


1 Answers

I had a similar problem. The way I went around it is by using the blur event to validate.

<input type="text" ng-model="yourModel" ng-blur="validateSelection()
  typeahead="item for item in list | filter:$viewValue"">
like image 153
GGhe Avatar answered Sep 05 '26 14:09

GGhe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!