javascript - Get Selected Text of Angular ng-option dropdown list - Stack Overflow

I have this drop-down list in my angular code:<div class="btn-group" dropdown><selec

I have this drop-down list in my angular code:

<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>

Now in my controller I can easily call the selected value as:

$scope.cleaningServiceLocation 

How can I get the text, or in my case, the name of the selected location?

I have this drop-down list in my angular code:

<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>

Now in my controller I can easily call the selected value as:

$scope.cleaningServiceLocation 

How can I get the text, or in my case, the name of the selected location?

Share Improve this question edited Mar 31, 2015 at 14:54 Austin Mullins 7,4372 gold badges35 silver badges48 bronze badges asked Mar 31, 2015 at 14:50 Kingsley SimonKingsley Simon 2,2107 gold badges44 silver badges89 bronze badges 1
  • If my answer helped you, you can mark it as answer so others know too. – Sam Commented Apr 1, 2015 at 11:39
Add a ment  | 

2 Answers 2

Reset to default 5

You can make model (perlocation) as object instead of (perlocation.id)-

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

And access it as -

$scope.cleaningServiceLocation.name

The easy way would be to loop through the locations array every time the value changes and grab the name property from the location whose id matches $scope.cleaningServiceLocation:

$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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744266806a4565905.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信