如何从下拉列表中获取选定的文本,值并将其分配到angularjs中的列表对象中

时间:2016-02-08 04:36:34

标签: html angularjs asp.net-web-api

这里我使用的是带有Web API的angularjs。我想获取选定的下拉列表值并分配到列表对象,然后它将保存到DB中。这是我的代码。

selectModule.controller("selectStateController", function ($scope) {
$scope.ILBasicInfoDTO.PYROrNonResidencyStateInfo.TaxpayerEarnedIncomeFromOtherState = { StateID: 10, StateName: 'Illinois' }, { StateID: 11, StateName: 'New York' }, { StateID: 12, StateName: 'Michigan' }, { StateID: 13, StateName: 'Georgia' }, { StateID: 14, StateName: 'California' };

$.ajax({
      url: serviceUrl + 'il/persistilbasicinfo',
      dataType: "json",
      type: "POST",
      async: true,            
      data: JSON.stringify({
                userId: userId,
                TaxReturnData:
                   {
                       UserDataId: userDataId,
                       TaxData: JSON.stringify($scope.ILBasicInfoDTO),
                       IsTaxMeterCall: false
                   },
            }),
            contentType: "application/json; charset=utf-8",
            error: function (jqXHR, textStatus, errorThrown) {
                ErrorHandling(jqXHR, errorThrown);
            },
            success: function (data, textStatus) {
                if (data != null) {

                }

            }
        })
});
<select id="ddlSpouseEearnedIncomeFromOtherState0" ng-model="ILBasicInfoDTO.PYROrNonResidencyStateInfo.TaxpayerEarnedIncomeFromOtherState">
<option value="">--Select--</option>
<option ng-repeat="TStatesExceptIL in TaxpayerStatesExceptILAndReciprocalStates" ng-value="TStatesExceptIL.StateId">{{TStatesExceptIL.StateCode}}</option>
</select>

1 个答案:

答案 0 :(得分:0)

要获取所选文本和值,请将整个对象传递为ng-value

<select id="ddlSpouseEearnedIncomeFromOtherState0" ng-model="ILBasicInfoDTO.PYROrNonResidencyStateInfo.TaxpayerEarnedIncomeFromOtherState">

 <option value="">--Select--</option>
 <option ng-repeat="TStatesExceptIL in TaxpayerStatesExceptILAndReciprocalStates" ng-value="TStatesExceptIL">{{TStatesExceptIL.StateCode}}</option>
</select>

现在,您可以从已绑定到$scope的{​​{1}}变量中获取文本和值,即ng-model

文字为:$scope.ILBasicInfoDTO.PYROrNonResidencyStateInfo.TaxpayerEarnedIncomeFromOtherState

值为:$scope.ILBasicInfoDTO.PYROrNonResidencyStateInfo.TaxpayerEarnedIncomeFromOtherState.StateCode