Can't set selected value of ng-options

后端 未结 4 483
自闭症患者
自闭症患者 2021-02-02 15:28

I am trying to populate a drop-down select options list and set a default selected value using ng-model and ng-options.

I have the following code in my view:

<         


        
4条回答
  •  你的背包
    2021-02-02 15:56

    Since you are using the entire object in your select then when Angular does it's comparison it is going to see if the objects are the same in order to set your select. I believe there is a way to change the functionality in how Angular does it's comparisions but I just loop through the select and do my own comparions similar to the below:

    $scope.siteList = [
            { id: 1, name: 'cycling'},
            { id: 2, name: 'walking'},
            { id: 3, name: 'holidays'}
        ]
    angular.forEach($scope.siteList, function(site, index) {
        if (site.id == 2) {
            $scope.thisTour.site = site;
        }
    });
    

    This will set the actual object to your variable allowing it to be set in the select.

提交回复
热议问题