ng-repeat with <option> binding issues #12005
Description
I have an odd problem where I cannot get an ng-model
value bound to a <select ng-model>
to work against a list bound with ng-repeat
. I can't duplicate the scenario in a simple plunkr.
This plunkr works, but the same code in an app with more complex layout fails with the first list displaying the wrong item in the first list. In my actual application, I see the model values displayed correctly on the bottom but the actual select list always selects DOG. IOW, the model is correct, but the binding in the select list is not.
The only way I've been able to work around this is to use $timeout() to delay assigning the value which works but is ugly:
// special handling for 'remembering' selection
var type = animalService.animalFilter.type;
vm.filter.type = '';
$timeout(function () {
vm.filter.type = type;
},30);
It appears that ng-model on the list is binding before the list is bound and depending on how quickly the page renders this causes a binding issue. The second list which uses static values bind correctly.
I have run into this a few times now, but it throws me off every single time until I remember that I need to delay binding. This really shouldn't be necessary - is there a better way to accomplish this or possibly a binding bug with a timing issue?