【发布时间】:2016-07-12 12:40:49
【问题描述】:
我正在尝试使用一个下拉列表的值来过滤下一个下拉列表中显示的值。下拉列表中填充了来自多个 JSON 文件的数据(如下所示)。
The desired result is to filter the Templates by Applications.Name, as you can see templates also has Application.Name inside of it, when the first dropdown is selected I would like the results to first be filtered to check if templates. Application.Name == selectedTestScript.Application(这是第一个下拉菜单的 ng-model)。
谁能指出一些有用资源的方向,或者更好地解释我哪里出错了?任何帮助是极大的赞赏。
应用程序 JSON:
{
"Applications": [
{"Id": 1, "Name":"Deep Thought"},
{"Id": 2, "Name":"Agent Smith"},
{"Id": 3, "Name":"Glados"},
{"Id": 4, "Name":"Jarvis"}
]
}
模板 JSON:
{
"Templates": [
{"Id": 1, "Name":"Deep Thought template 1", "Application":{"Name": "Deep Thought", "Id": 1}},
{"Id": 2, "Name":"Deep Thought template 2", "Application":{"Name": "Deep Thought", "Id": 1}},
{"Id": 3, "Name":"Agent Smith template 1", "Application":{"Name": "Agent Smith", "Id": 2}},
{"Id": 4, "Name":"Agent Smith template 2", "Application":{"Name": "Agent Smith", "Id": 2}},
{"Id": 5, "Name":"Glados template 1", "Application":{"Name": "Glados", "Id": 3}},
{"Id": 6, "Name":"Glados template 2", "Application":{"Name": "Glados", "Id": 3}},
{"Id": 7, "Name":"Jarvis template 1", "Application":{"Name": "Jarvis", "Id": 4}},
{"Id": 8, "Name":"Jarvis template 2", "Application":{"Name": "Jarvis", "Id": 4}}
]
}
HTML:
<div class="panel-body">
<div>
<label for="appName" class="control-label col-xs-3">Application:</label>
<div class="col-xs-9">
<select id="appName" class="form-control col-sm-4" placeholder="Please select an application" ng-model="selectedTestScript.Application" ng-options="application.Name for application in applications" />
</div>
</div>
<div>
<label retinafor="importActions" class="control-label col-xs-3">Templates:</label>
<div class="col-xs-9">
<div class="input-group">
<select class="form-control" placeholder="Please select an action" ng-model="selectedTemplate" ng-options="template.Name for template in templates | filter :{templates : templatesFilter}" />
<div class="input-group-btn">
<button type="button" class="btn btn-default btn-general" ng-click="importTemplate(selectedTemplate); actionList = true"><i class="fa fa-plus iconswhite"></i> Import</button>
</div>
</div>
</div>
</div>
</div>
控制器:
$scope.templatesFilter = function (application) {
return templates.Application.Name === $scope.selectedTestScript.Application;
}
【问题讨论】:
-
我想你只是忘记了“.Name”:$scope.selectedTestScript.Application.Name。此外,我认为使用 Id 而不是名称会更好......
-
起初,
<select>不是一个自闭合标签。 -
进行了这两项更改,(不敢相信我之前没有注意到
标签: javascript arrays angularjs json drop-down-menu