在ng-repeat和ng-change中传递两个值

时间:2017-08-29 10:34:01

标签: angularjs angularjs-ng-repeat angularjs-ng-change

控制器:

var response = {
  "json": {
    "response": {
      "servicetype": "100",
      "functiontype": "101",
      "statuscode": "success",
      "data": [
        {
          "countryid": 1,
          "countryname": "India",
          "countrycode": "91",
          "currencyname": "Indian rupee",
          "currencycode": "INR",
          "latitude": "21.7679",
          "longitude": "78.8718"
        }
      ]
    }
  }
}

  $scope.country = response.json.response.data;

HTML:

 <select name="Country" class="form-control1 drop countries" required  ng-model="model.country" placeholder="Select" value="India"  ng-change="getState(model.country);">
    <option value="" disabled selected> Country*</option>
    <option ng-repeat="item in country track by $index" value="{{item.countryid}}">{{item.countryname}}</option>
        </select>

我想传递countryname和countryid来获取状态列表,需要解决方案。我只能通过国家ID。需要帮助。

2 个答案:

答案 0 :(得分:2)

<select ng-model="country" required
    ng-options="c as c.countryname for c in country track by c.countryid" ng-change="getState(country.countryid, country.countryname);" >
    <option value="">Country*</option>
</select>

使用ng-options,您将获得国家/地区模型中的所有值

答案 1 :(得分:1)

改为使用ng-options

 <select  ng-model="model.country" required
    ng-options="c.countryid as c.countryname for c in country" >
    <option value="">Country*</option>
</select>

它会给你输出:

<select ng-model="model.country" required="" 
       ng-options="c.countryid as c.countryname for c in country" 
       class="ng-pristine ng-invalid ng-invalid-required">
    <option value="" class="">Country*</option>
    <option value="0">India</option>
</select>

Demo Fillde

关于您的案例:track by $index会破坏您的select

Demo Fillde 2