简体   繁体   中英

Select particular item in SELECT drop-down using angularjs when initialized from object array

I have a code of SELECT element like this

 select(ng-model="chosenProject" id="chosenProject"  ng-options="c.name for c in selectItems")

where selectItems are arrays of objects , and each object got name and value fields. Later in chosenProject.value I read an object (with value and name fields) of chosen item.

Now I also need to pre-select some element based on its value. If I do $scope.chosenProject.value=*somevaluenumber* , $scope.chosenProject.value.value =*somevaluenumber* it does not seem to work.

I can not change the declaration; chosenProject gets the whole object of selected item and that's what I use later in code.

Try this

angular.forEach($scope.selectItems, function(value){
    if(value.value == someValue){
          $scope.chosenProject = value;
          $scope.$apply();
    }
})

Demo: http://plnkr.co/edit/FSO2zi96AUbVpWG3v092?p=info

如果要为每个选项设置一个值 ,则应尝试使用此方法: 如何在AngularJS的ng-options中设置value属性?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM