Angularjs 使用ng重复选择中的预选选项
当我使用ng repeat重复选项时,如何预选选项,如下所示:Angularjs 使用ng重复选择中的预选选项,angularjs,Angularjs,当我使用ng repeat重复选项时,如何预选选项,如下所示: <select data-ng-model="info.country" ng-change="changeCountry()" name="country" required> <!-- <option selected disabled hidden value=''></option> --> <option data-ng-repeat="country in
<select data-ng-model="info.country" ng-change="changeCountry()" name="country" required>
<!-- <option selected disabled hidden value=''></option> -->
<option data-ng-repeat="country in countries" value="{{country.id}}">{{country.countryName}}</option>
</select>
{{country.countryName}
data ng model
返回编号3
,方法ng change=“changeCountry()”
在其实现中使用相同的编号
我需要预选的项目是3
,而不是1
(这将减少1,因为它是一个数组,但我想你明白了)
我需要预先选择项目。这是可能的吗?怎样才能达到这样的结果
我通过使用
ng选项实现了这一点:
<select ng-options="country.id as country.countryName for country in countries" ng-model="info.country" ng-change="changeCountry()" name="country" required></select>
<select data-ng-model="info.country"
ng-change="changeCountry()" name="country"
ng-options="c.id as c.countryName for c in countries"
required>
</select>
ng repeat
on
将id
转换为字符串。这就是它不起作用的原因
{“国家”:3}
vs{“国家”:“3}
您可以使用ng选项解决此问题
:
<select ng-options="country.id as country.countryName for country in countries" ng-model="info.country" ng-change="changeCountry()" name="country" required></select>
<select data-ng-model="info.country"
ng-change="changeCountry()" name="country"
ng-options="c.id as c.countryName for c in countries"
required>
</select>
你能分享一下国家/地区吗
example@MaximShoustin-是的,这从根本上解释了问题。谢谢你的澄清!真是太好了。