In angular the use of Track by to track selection by value instead of by reference is a nice feature if needed. But lets say I switch the array behind and the selected object id matches a new Id I want the select to change as well
How do I force it to reevaluate?
HTML:
<select ng-options="option.id for option in collection track by option.id" ng-model="selectedObject"></select>
<button ng-click="updateCollection()">Update</button>
JS:
$scope.collection = [{
id: 1,
value: 0
}, {
id: 2,
value: 0
}, {
id: 3,
value: 0
}];
$scope.selectedObject = $scope.collection[0];
$scope.updateCollection = function () {
var newColl = [{
id: 1,
value: 100
}, {
id: 2,
value: 200
}, {
id: 3,
value: 300
}];
$scope.collection = newColl;
Lets say I pick Id 2
and then update the selectedObject
is still the old id:2 value:0
, I want it to change straight into id:2 value:200
.
https://jsfiddle.net/IngoVals/p4ye3uaq/
P.S. Why doesn't the angular.copy
work in the fiddle?