无法在angularJs中设置下拉列表的选定值 [英] Unable to Set Selected Value Of DropDown In angularJs
本文介绍了无法在angularJs中设置下拉列表的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DropDown.我在Option上使用ng-repeat
绑定它的值.
我只想使用value字段设置选定的值.
I have a DropDown. I'm binding it's value using ng-repeat
on Option.
I want to Set the selected value using the value field only .
这是我的代码.
<div ng-controller="myCtrl">
<select ng-model="selectedItemvalue">
<option value="">--Select --</option>
<option ng-repeat="sel in selectables" value="{{sel.value}}">{{sel.label}}</option>
</select>
<p>Selected Value is : {{selectedItemvalue}}</p>
</div>
Js
angular.module('myApp', [])
// controller here
.controller('myCtrl', function($scope) {
$scope.selectables = [
{ label: 'A', value: 1},
{ label:'B', value: 2},
{ label: 'C', value: 3}
];
// this is the model that's used for the data binding in the select directive
// the default selected item
//using value, i want to set the selected value
$scope.selectedItemvalue = "2";
})
如您所见,我只想通过设置Value来设置选定的值.
As you can see, I want to set selected value uisng only By setting Value.
推荐答案
您需要使用ng-model
而不是ng-value
来将其绑定到模型.
You need to use ng-model
instead of ng-value
to bind it to model.
<select ng-model="selectedItemvalue">
更新:$scope.selectedItem
必须在控制器中为$scope.selectedItemvalue
,然后您可以使用ng-selected
匹配其条件
Update: $scope.selectedItem
needs to be $scope.selectedItemvalue
in controller and then you can use ng-selected
to match condition on it
工作演示
angular
.module('myApp', [])
// controller here
.controller('myCtrl', function($scope) {
$scope.selectables = [
{ label: 'A', value: 1},
{ label:'B', value: 2},
{ label: 'C', value: 3}
];
// this is the model that's used for the data binding in the select directive
// the default selected item
//using value, i want to set the selected value
$scope.selectedItemvalue = "2";
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div ng-controller="myCtrl" ng-app="myApp">
<select ng-model="selectedItemvalue">
<option value="">--Select --</option>
<option ng-repeat="sel in selectables" ng-selected="selectedItemvalue == sel.value" value="{{sel.value}}">{{sel.label}}</option>
</select>
<p>Selected Value is : {{selectedItemvalue}}</p>
</div>
这篇关于无法在angularJs中设置下拉列表的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文