gpt4 book ai didi

javascript - angularjs - 级联选择使用第一个选择中的模型数据

转载 作者:行者123 更新时间:2023-12-03 08:22:33 24 4
gpt4 key购买 nike

我试图获取存储在服务器模型中的版本数据,但是,它没有起作用。

我的假设是,在加载时,第一次选择的初始数据尚不可用,因为它尚未真正被选择(从技术上讲)。我尝试使用 trigger('click') 这是否有帮助,但它绝对没有任何作用。我似乎找不到任何答案,而且我有一种感觉,我可能从错误的一端解决了这个问题。

编辑:忘了提及,如果我必须重组我的数据以允许这种情况发生,那就这样吧。

这是我的所有代码+ JSFiddle:

http://jsfiddle.net/h8uoy9xr/3/

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div ng-app>
<div ng-controller="MyCntrl">
<div class="selection" ng-repeat="selection in selections">
<select ng-model="server" ng-options="server as server.name for server in servers track by server.id" ng-init="server.id=selection.server"></select>
{{ server | json }}
<select ng-model="version" ng-options="version as version.name for version in server.version track by version.id" ng-init="version.id=selection.version"></select>
</div>
</div>
</div>

JS:

function MyCntrl($scope) {

$scope.servers = [
{
"id": 1,
"name": "server1",
"version":
[
{
id:1,
name: "10.x"
},
{
id:3,name: "12.x"
}
]
},
{
"id": 2,
"name": "server2",
"version":
[
{
id: 2,
name: "1.0"
},
{
id: 3,
name: "2.0"
}
]
}
];

$scope.selections = [
{
server: 2,
version: 3
},
{
server: 1,
version: 3
}
];

}

cascading dropdownlist inside ng-repeat

最佳答案

这是上述问题的有效解决方案。我没有意识到 AngularJS 对模型的处理方式略有不同,但最终它成功了。对于将来需要类似内容的人,请参阅下面的代码+ fiddle :

fiddle :http://jsfiddle.net/mmy6z57g/2/

fiddle :http://jsfiddle.net/mmy6z57g/3/ (添加了一个按钮来演示如何添加其他服务器)

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div ng-app="test">
<div ng-controller="MyCntrl">
<div class="selection" ng-repeat="xserver in xservers">
<select ng-model="xservers[$index]" ng-options="server as server.name for server in servers track by server.id"></select>
<select ng-model="xversions[$index]" ng-options="version as version.name for version in xservers[$index].version track by version.id"></select>
</div>
</div>
</div>

JS:

var app = angular.module('test', []);

app.controller('MyCntrl', function($scope) {
$scope.servers = [
{
id: 1,
name: "server1",
version:
[
{
id:1,
name: "10.x"
},
{
id:3,
name: "12.x"
}
]
},
{
id: 2,
name: "server2",
version:
[
{
id: 2,
name: "1.0"
},
{
id: 3,
name: "2.0"
}
]
}
];

$scope.xservers = [
{
id: 2,
name: "server2",
version:
[
{
id: 2,
name: "1.0"
},
{
id: 3,
name: "2.0"
}
]
},
{
id: 1,
name: "server1",
version:
[
{
id:1,
name: "10.x"
},
{
id:3,
name: "12.x"
}
]
}
];

$scope.xversions = [
{
id: 3,
name: "2.0"
},
{
id: 3,
name: "12.x"
}
];
});

关于javascript - angularjs - 级联选择使用第一个选择中的模型数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33683269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com