【发布时间】:2016-08-21 23:31:38
【问题描述】:
现在已经搜索了几个小时,试图了解这是如何工作的。
但实际上我已经得到了
<div class="form-group">
<?php $field = 'member_address_country'; ?>
<label for="<?=$field?>">Country</label>
<select class="form-control" id="<?=$field?>" ng-options="country.country_id as country.country_name for country in countries track by country.country_id" ng-model="a.<?=$field?>">
<option value="" disabled="disabled" selected="selected">-- Select --</option>
</select>
</div>
我有一个国家数组,存储在$scope.countries 中,对象看起来类似于
[
{country_id: "1", country_name: "england"},
{country_id: "2", country_name: "america"},
{country_id: "3", country_name: "other"},
]
我有一个地址数组,它们在标记为a 的 ngRepeat 中,所以a.member_address_country 是一个字符串,其中包含一个 int id。在这个例子中是"2"
因此,尽管我使用 ngOptions 和 track by country.country_id 并且模型的值是来自 country_id 的 id,正如预期的那样,当呈现 ngRepeat 时它只显示 -- Select -- 而不是从数组中选择对象,基于跟踪的 id。
我做错了什么,我该如何解决?
我已经添加了问题的代码笔 -> http://codepen.io/owenmelbz/pen/XdxrMO?editors=1010
这里还有另一个例子 ->
angular.module('app', [])
.controller('testController', function($scope) {
$scope.a = {
member_address_country: 100
}
$scope.countries = [{
country_id: 100,
country_name: "england"
}, {
country_id: 200,
country_name: "uk"
}, {
country_id: 300,
country_name: "london"
}]
});
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.min.js"></script>
<div class="form-group" ng-app="app" ng-controller="testController">
<label for="member_address_country">Country</label>
<select class="form-control" id="member_address_country" ng-options="country.country_id as country.country_name for country in countries track by country.country_id" ng-model="a.member_address_country">
</select>model value is: {{a.member_address_country | json}}
</div>
【问题讨论】:
-
您说您的国家/地区数组存储在一个名为
$scope.country的变量中,但在您的代码中,您正试图绑定到一个countries变量。哪个是正确的? -
抱歉打错了,它的
$scope.countries你可以在codepen.io/owenmelbz/pen/XdxrMO?editors=1010看到它发生
标签: angularjs select ng-repeat ng-options