Why Angular ng-options with 'track by' can't selected by id

前端 未结 4 1924
眼角桃花
眼角桃花 2021-01-12 23:57

I have a array of objects that I\'m displaying using ng-options, and set ng-model with id. If i add track by on it, the item can\'t selected, if not add, it wor

4条回答
  •  无人共我
    2021-01-13 00:29

    As explained in this github issue - "ng-options track by and select as are not compatible" and shown in this fiddle

    "you just can't combine value as label for collection with track by. You have to pick the one or the other.".

    For your case, the select should be:

    
    

    and in JS

    $scope.country = $scope.countryList[0];
    

    JSFiddle here for your case.

    The value selected will be the object and not just the id alone.

    Again the point is, you have to chose between select as label or the track by case.

提交回复
热议问题