Model does not persist using uib-typeahead with ng

2019-07-29 19:50发布

问题:

I am trying to use uib-typeahead to select one or more objects (via an ng-repeat), but facing an issue where the model does not persist when adding another object.

This is reproduced on Plnkr here (based on ui-bootstrap's demo). (Reproduce the error by selecting a state, then clicking "Select another". The previously selected state disappears!)

What is causing the model to not persist? How can this be fixed? Is it an issue with uib-typeahead? Or the ng-repeat scope?

NB: I am also using a custom ngModel formatter, but that doesn't appear to be causing any issue; the issue remains when removing the format-state directive.

I have tried setting ng-model-options, to no avail.

Help appreciated!

回答1:

I've went over your plunk and then tried to reproduce it and couldn't figure out what's the directive's problem with ng-repeat. maybe I'me missing something, maybe it's an issue, you might want to ask this question at their github issue tracker.

But I've found that by binding the model value to a model property rather than a model itself does make everything work fine.

Instead of

ng-model="selected" 

Use this

ng-model="selected.state" 

and it now works.