AngularJs - SELECT 中的 ng-model
AngularJs - ng-model in a SELECT
问题:
我的页面中有一个 SELECT 元素,其中填充了一个 ng-repeat
。它还有一个具有默认值的 ng-model
。
当我更改值时,ng-model
会适应,没关系。但是下拉列表在启动时显示一个空槽,应该选择默认值的项目。
代码
<div ng-app ng-controller="myCtrl">
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit">
<option ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
</select>
</div>
使用 JS:
function myCtrl ($scope) {
$scope.units = [
{'id': 10, 'label': 'test1'},
{'id': 27, 'label': 'test2'},
{'id': 39, 'label': 'test3'},
]
$scope.data = {
'id': 1,
'unit': 27
}
};
试试下面的代码:
在你的控制器中:
function myCtrl ($scope) {
$scope.units = [
{'id': 10, 'label': 'test1'},
{'id': 27, 'label': 'test2'},
{'id': 39, 'label': 'test3'},
];
$scope.data= $scope.units[0]; // Set by default the value "test1"
};
在您的页面中:
<select ng-model="data" ng-options="opt as opt.label for opt in units ">
</select>
您可以在选项元素上使用 ng-selected 指令。它表示如果 truthy 将设置所选 属性.
在这种情况下:
<option ng-selected="data.unit == item.id"
ng-repeat="item in units"
ng-value="item.id">{{item.label}}</option>
演示
angular.module("app",[]).controller("myCtrl",function($scope) {
$scope.units = [
{'id': 10, 'label': 'test1'},
{'id': 27, 'label': 'test2'},
{'id': 39, 'label': 'test3'},
]
$scope.data = {
'id': 1,
'unit': 27
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit">
<option ng-selected="data.unit == item.id" ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
</select>
</div>
Select 的默认值应该是它在列表中的值之一。为了使用默认值加载 select,您可以使用 ng-options。需要在控制器中设置范围变量,并且该变量在 HTML 的 select 标记中指定为 ng-model。
查看此 plunker 以获取任何参考资料:
你不需要定义选项标签,你可以使用 ngOptions 指令来做到这一点:https://docs.angularjs.org/api/ng/directive/ngOptions
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit" ng-options="unit.id as unit.label for unit in units"></select>
However, ngOptions provides some benefits such as reducing memory and increasing speed by not creating a new scope for each repeated instance. angular docs
替代解决方案是使用 ng-init
指令。您可以指定将初始化默认数据的函数。
$scope.init = function(){
angular.forEach($scope.units, function(item){
if(item.id === $scope.data.unit){
$scope.data.unit = item;
}
});
}
您还可以将具有默认值的项目从 ng-repeat 中选择出来,如下所示:
<div ng-app="app" ng-controller="myCtrl">
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit">
<option value="yourDefaultValue">Default one</option>
<option ng-selected="data.unit == item.id" ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
</select>
</div>
并且不要忘记 value 属性,如果将其留空,您将遇到同样的问题。
问题:
我的页面中有一个 SELECT 元素,其中填充了一个 ng-repeat
。它还有一个具有默认值的 ng-model
。
当我更改值时,ng-model
会适应,没关系。但是下拉列表在启动时显示一个空槽,应该选择默认值的项目。
代码
<div ng-app ng-controller="myCtrl">
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit">
<option ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
</select>
</div>
使用 JS:
function myCtrl ($scope) {
$scope.units = [
{'id': 10, 'label': 'test1'},
{'id': 27, 'label': 'test2'},
{'id': 39, 'label': 'test3'},
]
$scope.data = {
'id': 1,
'unit': 27
}
};
试试下面的代码:
在你的控制器中:
function myCtrl ($scope) {
$scope.units = [
{'id': 10, 'label': 'test1'},
{'id': 27, 'label': 'test2'},
{'id': 39, 'label': 'test3'},
];
$scope.data= $scope.units[0]; // Set by default the value "test1"
};
在您的页面中:
<select ng-model="data" ng-options="opt as opt.label for opt in units ">
</select>
您可以在选项元素上使用 ng-selected 指令。它表示如果 truthy 将设置所选 属性.
在这种情况下:
<option ng-selected="data.unit == item.id"
ng-repeat="item in units"
ng-value="item.id">{{item.label}}</option>
演示
angular.module("app",[]).controller("myCtrl",function($scope) {
$scope.units = [
{'id': 10, 'label': 'test1'},
{'id': 27, 'label': 'test2'},
{'id': 39, 'label': 'test3'},
]
$scope.data = {
'id': 1,
'unit': 27
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit">
<option ng-selected="data.unit == item.id" ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
</select>
</div>
Select 的默认值应该是它在列表中的值之一。为了使用默认值加载 select,您可以使用 ng-options。需要在控制器中设置范围变量,并且该变量在 HTML 的 select 标记中指定为 ng-model。
查看此 plunker 以获取任何参考资料:
你不需要定义选项标签,你可以使用 ngOptions 指令来做到这一点:https://docs.angularjs.org/api/ng/directive/ngOptions
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit" ng-options="unit.id as unit.label for unit in units"></select>
However, ngOptions provides some benefits such as reducing memory and increasing speed by not creating a new scope for each repeated instance. angular docs
替代解决方案是使用 ng-init
指令。您可以指定将初始化默认数据的函数。
$scope.init = function(){
angular.forEach($scope.units, function(item){
if(item.id === $scope.data.unit){
$scope.data.unit = item;
}
});
}
您还可以将具有默认值的项目从 ng-repeat 中选择出来,如下所示:
<div ng-app="app" ng-controller="myCtrl">
<select class="form-control" ng-change="unitChanged()" ng-model="data.unit">
<option value="yourDefaultValue">Default one</option>
<option ng-selected="data.unit == item.id" ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
</select>
</div>
并且不要忘记 value 属性,如果将其留空,您将遇到同样的问题。