我可以直接从数据库填充2个下拉列表。我的问题是,必须根据第一个下拉选择填充第二个下拉值。由于我是Angular的新手,我无法弄明白,有人可以帮我解决这个问题。
<select id="OfficeId" name="OfficeId" ng-model="item.OfficeId"
ng-options="item.OfficeId as item.OfficeName for item in Offices">
<option value="" selected>Select the Office</option>
</select>
<select id="OBJ" name="OBJ" ng-model="item.OfficeOBJId"
ng-options="item.OfficeOBJId as item.OBJId for item in Codes">
<option value="" selected>Select OBJ Code</option>
</select>
myApp.factory('OfficeNames', function ($resource) {
return $resource(
'api/Office/:id',
{ id: '@id' },
{ update: { method: 'PUT' } }
);
});
myApp.factory('OfficeObjCode', function ($resource) {
return $resource(
'api/OfficeObj/:id',
{ id: '@id' },
{ update: { method: 'PUT' } }
);
});
function OfficeCtrl($scope, $location, OfficeNames) {
$scope.Offices = OfficeNames.query();
}
function OfficeObjCtrl($scope, $location, OfficeObjCode) {
$scope.Codes = OfficeObjCode.query();
}
注意:我使用的是Web API / Angular / Petapoco / SQL Server
答案 0 :(得分:14)
你不应该需要2个控制器,事实上这可能是其中一个问题。
一旦它们在同一范围内,您就可以轻松地将它们连接在一起。在第一个选项上使用ng-change
来触发一个函数,该函数获取填充第二个选项的值。
示例小提琴:http://jsfiddle.net/TheSharpieOne/Xku9z/
此外,您可以将ng-show
与第二个选项的选项数组长度一起使用,以仅在填充时显示第二个选择。
答案 1 :(得分:2)
这样做:
<select id="OfficeId" name="OfficeId" ng-model="item.OfficeId"
ng-options="item.OfficeId as item.OfficeName for item in Offices" ng-change="updateObjects(item.OfficeId)">
<option value="" selected>Select the Office</option>
<select id="OBJ" name="OBJ" ng-model="item.OfficeOBJId"
ng-options="item.OfficeOBJId as item.OBJId for item in Codes">
<option value="" selected>Select OBJ Code</option>
使用javascript,你只需要一个控制器,办公室控制器:
angular.module('myApp',[])
.controller('OfficeCtrl',
['$scope','$location','OfficeNames','OfficeObjCode',function($scope,
$location, OfficeNames,OfficeObjCode)
{
$scope.Offices=OfficeNames.query();
$scope.updateObjects =function(office_id)
{`// take {{office_id}} to server to pick codes based
//on that office id and
// then assign them to $scope.codes..`
$scope.Codes = 'what you came back with';
}
}).factory('OfficeNames', function ($resource) {
return $resource(
'api/Office/:id',
{ id: '@id' },
{ update: { method: 'PUT' } }
);
}).factory('OfficeObjCode', function ($resource) {
return $resource(
'api/OfficeObj/:id',
{ id: '@id' },
{ update: { method: 'PUT' } }
);
});
答案 2 :(得分:2)
我实施了如下,希望它有所帮助:)
控制器代码
var app = angular.module("app",[]);
app.controller("ctrl",function($scope){
$scope.key_options = [
{table:"Table_1",key:"age_flg",key_label:"Age"},
{table:"Table_1",key:"ethnicity_flg",key_label:"Ethnicity"},
{table:"Table_2",tab_label:"Table 2",key:"green_flg",key_label:"Green Flag"},
{table:"Table_2",tab_label:"Table 2",key:"life_flg",key_label:"Life Flag"}
];
$scope.options = [
{table:"Table_1",tab_label:"Table 1"},
{table:"Table_2",tab_label:"Table 2"}
];
$scope.sel = function(){
if($scope.key_attr){
console.log($scope.sel_attr['table']);
console.log($scope.key_attr['key']);
};
}
});
html代码:
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<script src="angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="ctrl">
<select ng-model="sel_attr" ng-init="sel_attr = options[0]" ng-options="attr['tab_label'] for attr in options"></select>
<select ng-model="key_attr" ng-init="key_attr = key_options[0]" ng-options="attr['key_label'] for attr in key_options |filter: {table: sel_attr['table']}" ng-change="sel()"></select>
</body>
</html>