大家好>
我试图从按钮单击的选择框中获取选项值,但它在控制台中显示未定义.Options值来自服务器
这是我的HTML代码
<div class="form-group">
<label class="control-form" for="cityid">Selct City</label>
<select type="text" class="form-control" placeholder="City" id="acity" >
<option value="">--Select City--</option>
<option ng-repeat="city in cityinfo" ng-value="{{city.id}}" ng-selected="{{city.id ==cityid}}">{{city.cityname}}</option>
</select>
<button class="btn btn-info prevnext pull-right" ng-click="nextpage()">Next <i class="fa fa-arrow-right"></i></button>
Controller.js文件代码
$scope.nextpage = function(pageno) {
console.log($scope.cityinfo);
}
提前致谢
答案 0 :(得分:9)
使用ng-options
代替ng-repeat
。
像这样:
<强>更新强>
<select type="text" class="form-control" placeholder="City" id="acity" ng-options="city.id as city.cityname for city in cityinfo track by city.id" ng-model="selectedCity">
<option value="">--Select City--</option>
</select>
<button class="btn btn-info prevnext pull-right" ng-click="nextpage(selectedCity)">Next <i class="fa fa-arrow-right"></i></button>
JS:
$scope.nextpage = function(selectedCity){
console.log(selectedCity);
}
答案 1 :(得分:1)
以下是示例代码。
Html代码
<select type="text" class="form-control" placeholder="City" id="acity" ng-options="city.id as city.cityname for city in cityinfo track by city.id" ng-model="currentCity">
<option value="">--Select Cities--</option>
</select>
<button class="btn btn-info prevnext pull-right" ng-
click="nextpage(currentCity)">Next <i class="fa fa-arrow-right"></i>
</button>
JS功能
$scope.nextpage = function(currentCity){
console.log(currentCity);
}