最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论