2015-03-31 104 views
4

我在我的角度代码此下拉列表:获取角NG选项下拉列表中选定的文本

<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 

我怎样才能得到文本,或在我的情况下,选定的位置的名称?每次

+0

如果我的回答帮你,你可以将其标记为回答让其他人也知道。 – Sam 2015-04-01 11:39:08

回答

2

最简单的方法是将遍历数组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 ""; 
} 

Try it in a Plunker

4

你可以让模型(perlocation)为对象,而不是(perlocation.id) -

<select class="selected_location" ng-options="perlocation as perlocation.name for perlocation in locations" ng-model="cleaningServiceLocation"> 

,并获得它 -

$scope.cleaningServiceLocation.name