我有一个看起来像这样的json
[{"partner_id":"8","partner_name":"Company1","partner_location":["Place1","Place2","Place3"],"partner_user":["User1","User2","User3"]},{"partner_id":"9","partner_name":"Company2","partner_location":["Place4","Place5"],"partner_user":["User4","User5"]}]
现在我想做这样的事情。我有2个下拉菜单,我希望第一个用我的2个列表中的partner_name填充,所以它必须看起来像这样
<select>
<option value="8">Company1</option>
<option value="9">Company2</option>
</select>
但我想要使用我在service_id
内设置的值来选择它。
我知道你可以这样做这个
<select ng-options="partner for partner in jsonPartners" ng-model="service_id"></select>
但是当我这样做时,我的选择2选项名为[object Object]
第二个选择更棘手,我希望它能够在我的第一个选择中选择id的位置。因此,如果我选择Company1
,则应选择Place1, Place2, Place3
,如果我选择了Company2
,则应填充Place4, Place5
。
先谢谢你,丹尼尔!
答案 0 :(得分:4)
<div ng-app="myApp" ng-controller="ctrl">
<select ng-options="partner as partner.partner_name
for partner in jsonPartners" ng-model="service_id"></select>
<select ng-options="place for place in service_id.partner_location"
ng-model="service_location"></select>
</div>
var app = angular.module('myApp', []);
function ctrl($scope){
$scope.jsonPartners = [{"partner_id":"8", ..},{"partner_id":"9", ..}]
$scope.service_id = $scope.jsonPartners[1];
}
答案 1 :(得分:2)
我会添加ng-change
以默认选择第一项:
JS控制器
$scope.jsonPartners = [{
"partner_id": "8",
"partner_name": "Company1",
"partner_location": ["Place1", "Place2", "Place3"],
"partner_user": ["User1", "User2", "User3"]
}, {
"partner_id": "9",
"partner_name": "Company2",
"partner_location": ["Place4", "Place5"],
"partner_user": ["User4", "User5"]
}];
$scope.partner = $scope.jsonPartners[0];
$scope.place = $scope.partner.partner_location[0];
$scope.onChange = function(partner){
$scope.place = partner.partner_location[0];
}
<强> HTML 强>
<div ng-controller="fessCntrl">
<select ng-model="partner"
ng-options="partner as partner.partner_name for partner in jsonPartners"
ng-change="onChange(partner)"
></select>
<select ng-model="place"
ng-options="place as place for place in partner.partner_location"
></select>
</div>
演示 Fiddle