我已经看到无数的帖子尝试在下拉列表中默认选择一个选项。但我一直没能实现它。我有一些国家/地区的数组。
$scope.regions =
[
{
name: "COLOMBIA",
code: 5
},
{
name: "ARGENTINA",
code: 6
},
{
name: "BRAZIL",
code: 7
}
];
我有这个变量:
$scope.selectThisId={
"id":6,
"animal":'dog',
"variable":'xxx32'
};
我需要下拉值等于
的 id 属性$scope.region=$scope.selectThisId.id;
变量。
最佳答案
工作演示:http://plnkr.co/edit/zhye91pDUEMgaZnXZGWE?p=preview
您基本上可以通过以下方式创建默认区域
$scope.select = $scope.selectThisId.id;
$scope.regionSelected = {};
然后使用 ng-options
创建一个 select
标签,将其绑定(bind)到模型,并在模型上调用 ng-init
。
<select ng-options="item as item.name for item in regions track by item.code"
ng-model="regionSelected" ng-init="regionSelected.code = select">
</select>
关于javascript - 在下拉列表中选择默认选项,将值保存在下拉列表的 value 属性中。 Angular.js。 Angular.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690979/