How to get selected value from select box in angularjs

前端 未结 2 1364
我在风中等你
我在风中等你 2021-02-10 03:16

Hello Everyone

I am trying to get option value from select box on button click but it shows undefined in console .Options value are coming from server <

相关标签:
2条回答
  • 2021-02-10 03:47

    Here is the sample code.

    Html code

     <select type="text" class="form-control" placeholder="City" id="acity" ng-options="city.id as city.cityname for city in cityinfo track by city.id" ng-model="currentCity">
        <option value="">--Select Cities--</option>
    </select>
     <button class="btn btn-info prevnext pull-right" ng- 
      click="nextpage(currentCity)">Next <i class="fa fa-arrow-right"></i> 
      </button>
    

    JS function

    $scope.nextpage = function(currentCity){
        console.log(currentCity);
    }
    
    0 讨论(0)
  • 2021-02-10 03:51

    Use ng-options instead of ng-repeat.

    Like this:

    Updated

    <select type="text" class="form-control" placeholder="City" id="acity" ng-options="city.id as city.cityname for city in cityinfo track by city.id" ng-model="selectedCity">
        <option value="">--Select City--</option>
    </select>
    <button class="btn btn-info prevnext pull-right" ng-click="nextpage(selectedCity)">Next <i class="fa fa-arrow-right"></i></button>
    

    JS:

    $scope.nextpage = function(selectedCity){
        console.log(selectedCity);
    }
    
    0 讨论(0)
提交回复
热议问题