Setting default value in select drop-down using Angularjs

前端 未结 10 1149
囚心锁ツ
囚心锁ツ 2020-12-03 05:24

I have an object as below. I have to display this as a drop-down:

var list = [{id:4,name:\"abc\"},{id:600,name:\"def\"},{id:200,name:\"xyz\"}]
10条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 05:41

    When you use ng-options to populate a select list, it uses the entire object as the selected value, not just the single value you see in the select list. So in your case, you'd need to set

    $scope.object.setDefault = {
        id:600,
        name:"def"
    };
    

    or

    $scope.object.setDefault = $scope.selectItems[1];
    

    I also recommend just outputting the value of $scope.object.setDefault in your template to see what I'm talking about getting selected.

    {{object.setDefault}}

提交回复
热议问题