2
在我的项目中,我需要从下拉列表中选择多个选项。我的代码看起来像这样:在AngularJs中从下拉列表中选择多个选项
//Controller
$scope.data = [{id: 1, Country: Zambia},
{id: 2, Country: United Kingdom}
{id: 3, Country: USA}]
$scope.selectedCountry = [];
//view
<select name="multipleSelect" id="multipleSelect" ng-model=" selectedCountry" multiple
ng-options="country.country as country.country for country in data"></select>
<h4> {{selectedCountry}}</h4>
上面的上下文工作,我可以从下拉列表中选择多个选项。这link帮助我实现了这一点。
问题所在。 现在我遇到的问题是,当我从下拉列表中选择选项时,我需要能够在selectedCountry数组中传递id和国家,而不仅仅是国家。因此,例如在上述情况下,当你选择第一个国家时,通过selectedCountry Array传递的信息将是这样的[“赞比亚”],但我真正想要的是这样的事情 [{id: 1, Country Zambia}].
为了解决这个问题,我试图做到这一点的看法:
<select name="multipleSelect" id="multipleSelect" ng-model=" selectedCountry" multiple
ng-options="{Id: country.Id, Country: country.country } as country.country for country in data"></select>
<h4> {{selectedCountry}}</h4>
运作良好,并传递给SELECTEDCOUNTRY阵列中的数据就像是{id: 1, Country: zambia}
的对象,但问题是,我不能选择从下拉菜单中的多个选项我只能选择1个选项。 我在做什么错?什么是实现这个目标的最好方法? 谢谢