【发布时间】:2018-06-12 06:28:44
【问题描述】:
我在 angularJS 中预选选择输入时遇到了一些问题。 选择框被一个数组填充。
<select class="form-control"
ng-model="userCtrl.selected_country"
ng-options="country.name for country in userCtrl.available_countries track by country.id">
</select>
例如一个选定的国家/地区填充了一个 ID(我通过 API 获得)。
userCtrl.selected_country = 4;
但这行不通。我也尝试过使用 options 和 ng-selected 来做到这一点。
<option ng-repeat="country in userCtrl.available_countries"
ng-selected="{{ country.id == userCtrl.selected_country }}"
value="{{ country.id }}">
{{ country.name }}
</option>
为什么我不能这样选择?这“有效”,在源代码中我看到正确的选项 selected="true" - 但在视图中你看不到它......非常奇怪。
我现在的“解决方案”是,用源数组“available_countries”的正确值填充我的 ng 模型。 不必要的许多步骤?!
- API 的加载数据(选择填充)
- 加载 API 的数据(地址)
- 将所有引用 ID(如 country_id、state_id)操作到 select-fill-objects(用于预选)
- 发生用户编辑 => 用户点击更新
- 将所有 select-fill-objects 改回 reference-id
- 立即保存地址
- 重复第 3 步
如果我可以仅使用数字进行预选,则不需要 3.、5. 和 7.。 这有可能吗?
感谢您的帮助 :-) 斯蒂芬
【问题讨论】:
-
我问了一些非常相似的问题 - stackoverflow.com/questions/33524138/… 但是关于对象问题,下面的答案是正确的。在这篇文章中阅读更多内容gurustop.net/blog/2014/01/28/…
标签: javascript angularjs angularjs-select