我想要实现的是,当我提交表单时,它应该返回我在控制台中选择的选项的标签,但它返回的是选项的值。
预期结果:
DXBA-天空
结果得到:
迪拜
的HTML
<div class="suggestField">
<input type='text' list='listid' placeholder="TO" ng-model="toCity" ng- change="suggestCity(toCity)">
<datalist id='listid'>
<option ng-repeat="item in fromSuggestData" label='{{item.PlaceId}}' value='{{item.PlaceName}}'>
</datalist>
<button ng-click="submitForm(toCity)">SUBMIT</button>
</div>
JS
$scope.fromSuggestData = {
"PlaceId": "DXBA-sky",
"PlaceName": "Dubai",
"CountryId": "AE-sky",
"RegionId": "",
"CityId": "DXBA-sky",
"CountryName": "United Arab Emirates"
}, {
"PlaceId": "DUJ-sky",
"PlaceName": "Dubois",
"CountryId": "US-sky",
"RegionId": "PA",
"CityId": "DUJA-sky",
"CountryName": "United States"
}
$scope.submitForm = function(toCity) {
console.log(toCity);
}
最佳答案
这是使用ng-option的演示。
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.fromSuggestData = [{
"PlaceId": "DXBA-sky",
"PlaceName": "Dubai",
"CountryId": "AE-sky",
"RegionId": "",
"CityId": "DXBA-sky",
"CountryName": "United Arab Emirates"
}, {
"PlaceId": "DUJ-sky",
"PlaceName": "Dubois",
"CountryId": "US-sky",
"RegionId": "PA",
"CityId": "DUJA-sky",
"CountryName": "United States"
}];
$scope.submitForm = function(toCity) {
console.log(toCity);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl" class="suggestField">
<select id="cityname" ng-model="toCity" ng-options="item as item.PlaceName for item in fromSuggestData">
<option value="">Select</option>
</select>
<button ng-click="submitForm(toCity.PlaceId)">SUBMIT</button>
</div>