[英]Get Selected Text of Angular ng-option dropdown list
我的角度代碼中有這個下拉列表:
<div class="btn-group" dropdown>
<select class="selected_location" ng-options="perlocation.id as perlocation.name for perlocation in locations" ng-model="cleaningServiceLocation">
<option value="">Please Select Location</option>
</select>
<div>
現在在我的控制器中,我可以輕松地將所選值調用為:
$scope.cleaningServiceLocation
如何獲取文本,或者在我的情況下,獲取所選位置的名稱?
您可以將模型(perlocation)設為對象而不是(perlocation.id) -
<select class="selected_location" ng-options="perlocation as perlocation.name for perlocation in locations" ng-model="cleaningServiceLocation">
並將其作為 -
$scope.cleaningServiceLocation.name
簡單的方法是在每次值更改時循環遍歷locations
數組,並從id
匹配$scope.cleaningServiceLocation
的位置獲取name
屬性:
$scope.getName = function(id) {
for (var i = 0; i < $scope.locations.length; i++) {
if ($scope.locations[i].id == id) {
return $scope.locations[i].name;
}
}
return "";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.