簡體   English   中英

獲取Angular ng-option下拉列表的選定文本

[英]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 "";
}

在Plunker中嘗試一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM