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

4

控制器:

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>

我想传递国家名称和国家ID以获取州列表,需要解决方案。我只能传递国家ID。需要协助。


你想将两个参数传递到ng-change中吗?这样行不行:ng-change="getState(model.country, model.id)"? - rrd
模型的id属性怎么办?我们如何分配它? - Nicoleta Wilskon
上一个有误。好的,所以getState()是你的控制器上的一个方法,model.country被传递进来,应该是你在下拉菜单中选择的模型,因此如果你想要countryname/countryid,getState()应该已经拥有了它所需的一切。 - rrd
2个回答

3
<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,您将在国家模型中获得所有值。

ng-change 对我不起作用。getState(item.id,item.country) 我得到了未定义的值。 - Nicoleta Wilskon
1
你能否只传递 getState(country)? - byteC0de
请检查您的模型名称,如果是国家,请更改为“项目”。 - byteC0de

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

可以翻译为:

{{链接1:Demo Fillde}}


关于您的问题: track by $index 破坏了您的 select演示 Fillde 2

但是我的问题是关于ng-change的,而且在你的回答中没有提到。你能帮我传递countryid和countryname吗? - Nicoleta Wilskon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接