2015-12-07 46 views
0
<div class="field-wrap"> 
    <select name=courses class="input-field1" name="state" title="state" ng-model="signup.state" > 
        <option value="">Select State</option> 
        <option value="Andaman and Nicobar Islands">Andaman and Nicobar Islands</option> 
        <option value="Andhra Pradesh">Andhra Pradesh</option> 
        <option value="Arunachal Pradesh">Arunachal Pradesh</option> 
        <option value="Assam">Assam</option> 
        <option value="Bihar">Bihar</option> 
        <option value="Chandigarh">Chandigarh</option> 
        <option value="Chhattisgarh">Chhattisgarh</option> 
        <option value="Dadra and Nagar Haveli">Dadra and Nagar Haveli</option> 
        <option value="Daman and Diu">Daman and Diu</option> 
        <option value="Delhi">Delhi</option> 
        <option value="Goa">Goa</option> 
        <option value="Gujarat">Gujarat</option> 
        <option value="Haryana">Haryana</option> 
        <option value="Himachal Pradesh">Himachal Pradesh</option> 
        <option value="Jammu and Kashmir">Jammu and Kashmir</option> 
        <option value="Jharkhand">Jharkhand</option> 
        <option value="Karnataka">Karnataka</option> 
        <option value="Kerala">Kerala</option> 
        <option value="Lakshadweep">Lakshadweep</option> 
        <option value="Madhya Pradesh">Madhya Pradesh</option> 
        <option value="Maharashtra">Maharashtra</option> 
        <option value="Manipur">Manipur</option> 
        <option value="Meghalaya">Meghalaya</option> 
        <option value="Mizoram">Mizoram</option> 
        <option value="Nagaland">Nagaland</option> 
        <option value="Orissa">Orissa</option> 
        <option value="Pondicherry">Pondicherry</option> 
        <option value="Punjab">Punjab</option> 
        <option value="Rajasthan">Rajasthan</option> 
        <option value="Sikkim">Sikkim</option> 
        <option value="Tamil Nadu">Tamil Nadu</option> 
        <option value="Tripura">Tripura</option> 
        <option value="Uttaranchal">Uttaranchal</option> 
        <option value="Uttar Pradesh">Uttar Pradesh</option> 
        <option value="West Bengal">West Bengal</option> 
        </select> 
     </div> 

這是我正在使用的選擇菜單我想要在我的控制器中獲取選定的值,但不知道如何做到這一點,我已經提取了文本框的值,但不知道選擇框。如何調用控制器中選定選項的值?

$scope.signupForm = function() { 
         if ($scope.signup_form.$valid) { 
          var data = { 
           firstname : $scope.signup_form.firstname.$modelValue, 
           lastname : $scope.signup_form.lastname.$modelValue, 
           email : $scope.signup_form.email.$modelValue, 
           mobileNumber : $scope.signup_form.mobileNumber.$modelValue, 
           district : $scope.signup_form.district.$modelValue, 
           state : $scope.signup_form.state.$modelValue, 
           course : $scope.signup_form.course.$modelValue, 
           collegeName : $scope.signup_form.collegeName.$modelValue, 

          }; 

這是我的控制器,我想我必須使用ng選項,但不能確定。 謝謝

回答

0

在你的情況$ scope.signup.state將具有選定狀態的值。 所選選項的值被保存到分配給下拉的模型中

+0

感謝您將檢查 – Aashima

0

我建議在控制器中添加一個選項數組,而不是在HTML中對它進行硬編碼,然後使用ng-options指令來顯示選項數組,你最終的東西,如:

<select name=courses class="input-field1" name="state" title="state" ng-model="signup.state" ng-options="option in options"> 

(哪裏是你的控制器內聲明的選項選項數組

$scope.options = ['option1', 'option2']; 

然後您可以輸入簡單地結合到NG-模型和定期使用它從控制器

$scope.signup.state 

會給你訪問任何選項被選中

+0

太感謝你了:) – Aashima

0

您可以使用ng-change這裏。當從下拉列表中選擇一個選項時,此方法將被觸發,並且我們可以保留選定的選項數據。

HTML

<div> 
    <select ng-model="selState" ng-change="selectedState(selState)" 
    data-ng-options="state as state.name for state in states"> 
     <option value="">Select Account</option> 
    </select> 
    {{selectedState}}  
</div> 

的script.js

myApp.controller('Ctrl', function($scope) { 
     $scope.states=[ 
         {name:"AP"}, 
         {name:"Karnataka"}, 
         {name:"Tamilnadu"} 
         ]; 

     $scope.selectedState = function(item) { 
      alert(item.name); 
     }; 

工作Plunker

+0

謝謝你會嘗試。 – Aashima

相關問題