Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 基于下拉列表1选择填充下拉列表2_Angularjs - Fatal编程技术网

Angularjs 基于下拉列表1选择填充下拉列表2

Angularjs 基于下拉列表1选择填充下拉列表2,angularjs,Angularjs,我能够直接从数据库填充2个下拉列表。我的问题是,必须根据第一个下拉选择填充第二个下拉值。因为我是个新手,我不知道,有人能帮我解决这个问题吗 <select id="OfficeId" name="OfficeId" ng-model="item.OfficeId" ng-options="item.OfficeId as item.OfficeName for item in Offices"> <option value="" selected>

我能够直接从数据库填充2个下拉列表。我的问题是,必须根据第一个下拉选择填充第二个下拉值。因为我是个新手,我不知道,有人能帮我解决这个问题吗

<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();
}

选择办公室
选择OBJ代码
myApp.factory('OfficeNames',函数($resource){
返回$resource(
“api/Office/:id”,
{id:'@id'},
{update:{method:'PUT'}}
);
});     
myApp.factory('OfficeObjCode',函数($resource){
返回$resource(
'api/OfficeObj/:id',
{id:'@id'},
{update:{method:'PUT'}}
);
});
职能OfficeCtrl($scope、$location、OfficeNames){
$scope.Offices=OfficeNames.query();
}
函数OfficeObjCtrl($scope、$location、OfficeObjCode){
$scope.code=OfficeObjCode.query();
}

注意:我使用的是Web API/Angular/Petapoco/SQL Server,您不需要两个控制器,事实上,这可能是问题之一。 一旦它们在同一范围内,您就可以轻松地将它们绑定在一起。在第一个选项上使用
ng change
来触发一个函数,该函数获取用于填充第二个选项的值

小提琴示例:

此外,您还可以将
ng show
与第二个select的选项数组长度一起使用,以便仅在填充第二个select时显示它

小提琴示例:

这样做:

<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>

我实现了如下内容,希望对您有所帮助:)

控制器代码

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>


欢迎来到SO!为了帮助我们更好地帮助您,我强烈建议您发布您已有的任何相关代码。在选择调用http.get的州并使用此Json格式后,我如何调整您的示例以填充城市:
$scope.states=[{“id”:“1',“name”:“Michigan',“cities”:[{id':'1','name':'Detroit'},{id':'2','name':'Flint'}]您需要一个州列表,然后是每个州的城市列表(基本上是50个城市列表,每个州1个列表)。选择该州后,您将在
$http.get
调用中使用该值来获取该州的城市列表。然后在范围内设置该列表。使用同样包含城市的州的单一列表,您不需要获取cites,因为您已经拥有了这些州的cites。如果
,请不要使用
ng。正如作者所说,在第二个下拉列表中使用
ng show
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']);
};
}
});
<!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>