I have seen infinity of post to try to choose by default an option in a dropdown. But I have not been able to achieve it. I have an array of some countries.
我已经看到无限的帖子试图在下拉列表中默认选择一个选项。但我无法实现它。我有一些国家。
$scope.regions =
[
{
name: "COLOMBIA",
code: 5
},
{
name: "ARGENTINA",
code: 6
},
{
name: "BRAZIL",
code: 7
}
];
I have this variable:
我有这个变量:
$scope.selectThisId={
"id":6,
"animal":'dog',
"variable":'xxx32'
};
I need the dropdown value to be equal to the id attribute of the
我需要下拉值等于。的id属性
$scope.region=$scope.selectThisId.id;
variable.
变量。
http://plnkr.co/edit/nmc8iLz6BIFac0Swfth8?p=preview
http://plnkr.co/edit/nmc8iLz6BIFac0Swfth8?p=preview
3 个解决方案
#1
4
Working demo: http://plnkr.co/edit/zhye91pDUEMgaZnXZGWE?p=preview
工作演示:http://plnkr.co/edit/zhye91pDUEMgaZnXZGWE?p = preview
You basically create a default region by doing
您基本上可以通过执行创建默认区域
$scope.select = $scope.selectThisId.id;
$scope.regionSelected = {};
then create a select
tag with ng-options
, binding it to the model, and calling ng-init
on the model.
然后使用ng-options创建一个选择标记,将其绑定到模型,并在模型上调用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>
#2
0
You can add a filter to your controller to get the default region (don't forget to add $filter
after $scope
):
您可以向控制器添加过滤器以获取默认区域(不要忘记在$ scope之后添加$ filter):
$scope.defaultValue = $filter('filter')($scope.regions, {code: $scope.selectThisId.id}, true)[0];
and then add this to your select in your view
然后将其添加到视图中的选择中
ng-init="select = defaultValue"
演示
#3
0
DEMO
var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
$scope.regions =
[
{
name: "COLOMBIA",
code: 5
},
{
name: "ARGENTINA",
code: 6
},
{
name: "BRAZIL",
code: 7
}
];
$scope.selectThisId={
"id":6,
"animal":'dog',
"variable":'xxx32'
};
$scope.region=$scope.selectThisId.id;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
<fieldset ng-controller="FirstCtrl">
<select
ng-options="item.code as item.name for item in regions"
ng-model="region"></select>
</fieldset>
</div>
#1
4
Working demo: http://plnkr.co/edit/zhye91pDUEMgaZnXZGWE?p=preview
工作演示:http://plnkr.co/edit/zhye91pDUEMgaZnXZGWE?p = preview
You basically create a default region by doing
您基本上可以通过执行创建默认区域
$scope.select = $scope.selectThisId.id;
$scope.regionSelected = {};
then create a select
tag with ng-options
, binding it to the model, and calling ng-init
on the model.
然后使用ng-options创建一个选择标记,将其绑定到模型,并在模型上调用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>
#2
0
You can add a filter to your controller to get the default region (don't forget to add $filter
after $scope
):
您可以向控制器添加过滤器以获取默认区域(不要忘记在$ scope之后添加$ filter):
$scope.defaultValue = $filter('filter')($scope.regions, {code: $scope.selectThisId.id}, true)[0];
and then add this to your select in your view
然后将其添加到视图中的选择中
ng-init="select = defaultValue"
演示
#3
0
DEMO
var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
$scope.regions =
[
{
name: "COLOMBIA",
code: 5
},
{
name: "ARGENTINA",
code: 6
},
{
name: "BRAZIL",
code: 7
}
];
$scope.selectThisId={
"id":6,
"animal":'dog',
"variable":'xxx32'
};
$scope.region=$scope.selectThisId.id;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
<fieldset ng-controller="FirstCtrl">
<select
ng-options="item.code as item.name for item in regions"
ng-model="region"></select>
</fieldset>
</div>