我试图获取“选择”的值,但它总是显示“未定义”。我想从 Controller 中将值设置为“B”,同时获取<select>
的值。我做错了什么?
<div ng-controller="myCtrl">
<select ng-model="selectedItemvalue" id='selectedItemvalue' ng-change='myfunction()'>
<option ng-repeat="sel in selectables" value="{{sel}}">{{sel}}</option>
</select>
</div>
angular.module('myApp', [])
// controller here
.controller('myCtrl', function($scope) {
$scope.selectables = [
'A','B','C'
];
//$scope.selectedItemvalue = "B";
console.log($scope.selectedItemvalue)
})
我需要获取选择的值。然后设置默认值“B”。 我需要将值设置为“B”而不运行 ng-change。我需要在 ng-change 之前获取我的选择值。
最佳答案
检查这个
<div ng-controller="myCtrl">
<select ng-model="selectedItemvalue" id='selectedItemvalue' ng-change='myfunction()'>
<option ng-selected="sel==selectedItemvalue" ng-repeat="sel in selectables" value="{{sel}}">{{sel}}</option>
</select>
<p>Selected Value is : {{selectedItemvalue}}</p>
</div>
angular.module('myApp', [])
// controller here
.controller('myCtrl', function($scope) {
$scope.selectables = [
'A','B','C'
];
$scope.selectedItemvalue = "B";
console.log($scope.selectedItemvalue)
})
关于javascript - <select> 的获取值显示为未定义。 (Angular.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354964/